c#取lua的文件 然后根据里边的table 创建相应物体

15 篇文章 0 订阅
13 篇文章 0 订阅

 自己的写的工具存一下,在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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值