自己的写的工具存一下,在c#中取lua的文件,在根据lua的table 创建相应的物体
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using LuaInterface;
using System.Linq;
using System.Text;
using System;
public class PaytableTools : MonoBehaviour {
public GameObject obj;
public GameObject parent;
public string table_name;
public void Creat_item()
{
Debug.Log (table_name);
string path = Application.dataPath + "/HotResources/LuaScript/Define";
LuaState state = new LuaState ();
state.Start();
state.AddSearchPath (path);
state.DoFile ("LineDefine.lua");
try {
LuaTable table = state.GetTable(table_name);
var aTable = table.ToArrayTable ();
var w = obj.transform.gameObject.GetComponent<RectTransform>().rect.width;
var h = obj.transform.gameObject.GetComponent<RectTransform>().rect.height;
var num = (decimal)aTable.Length/7;
int rows = int.Parse (System.Math.Ceiling(num).ToString());
int hight = (int)h * rows + 100;
parent.transform.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(0, hight);
parent.transform.gameObject.GetComponent<GridLayoutGroup>().cellSize = new Vector2(w, h);
for (int i = 1; i <= aTable.Length; i++)
{
var item = Instantiate (obj);
item.transform.SetParent(parent.transform);
item.transform.localScale = Vector3.one;
item.transform.gameObject.SetActive (true);
item.transform.Find ("Text").GetComponent<Text>().text = (i).ToString ();
item.transform.name = string.Format("{0}", i);
var line = aTable[i];
LuaTable final_table = line as LuaTable;
for (int j = 1; j <= final_table.Length; j++) {
object row = final_table [j];
int final_row = int.Parse (row.ToString ());
var index = (final_row - 1) * final_table.Length + j;
try {
item.transform.Find ("parent/" + index + "/image").gameObject.SetActive(true);
} catch (Exception ex) {
Debug.LogError ("看下是行和列对不对");
}
}
}
} catch (Exception ex) {
Debug.LogError ("看下是不是table_name写错了,要跟LineDefine里对的上");
}
state.Dispose ();
}
public void Delete()
{
try {
int i = 0;
while (true) {
DestroyImmediate (parent.transform.GetChild(i).gameObject);
}
} catch (Exception ex) {
Debug.LogError ("先创建在删除");
}
}
}
#endif