【ulua入门】(2) 动态实例化GameObject,然后更改UGUI元素的值,使用案件移动物体

来源:互联网 发布:白敬亭 演技 知乎 编辑:程序博客网 时间:2024/05/21 06:51

参考链接 : http://www.manew.com/thread-91203-1-1.html


在这里下载ulua : https://github.com/jarjin/LuaFramework_UGUI


这是第二篇关于ulua的博客,关于基础的部分,请看第一篇。


首先,我们新建一个 TestComponemt.cs 脚本,使用它来处理跟lua脚本的交互


using UnityEngine;using System.Collections;using LuaInterface;using LuaFramework;using UnityEngine.UI;namespace LuaFramework{    public class TestComponemt : MonoBehaviour    {        public LuaTable table;         //添加监听       public static void AddButtonClick(GameObject go, LuaFunction luafunc)        {              if (go == null || luafunc == null)                      return;              Button btn = go.GetComponent<Button> ();              if (btn == null)                     return;              btn.onClick.AddListener              (                     delegate()                      {                            luafunc.Call(go);                     }              );       }        public static void Text(GameObject go, string str)       {           if (go == null || str == null) return;           Text txt = go.GetComponent<Text>();           if (txt == null) return;           txt.text = str;       }       //清除监听       public static void ClearButtonClick(GameObject go)        {              if (go == null)                      return;              Button btn = go.GetComponent<Button> ();              if (btn == null)                     return;              btn.onClick.RemoveAllListeners();                     }        public static LuaTable Add(GameObject go, LuaTable tableClass)       {           LuaFunction fun = tableClass.GetLuaFunction("New");           if (fun == null)               return null;           object[] rets = fun.Call(tableClass);           if (rets.Length != 1) return null;           TestComponemt cmp = go.AddComponent<TestComponemt>();           cmp.table = (LuaTable)rets[0];           cmp.CallAwake();           return cmp.table;       }        public static LuaTable Get(GameObject go, LuaTable table)        {            TestComponemt[] cmps = go.GetComponents<TestComponemt>();            foreach (TestComponemt item in cmps)            {                string mat1 = table.ToString();                string mat2 = item.table.GetMetaTable().ToString();                if(mat1 == mat2)                {                    return item.table;                }            }            return null;        }        void CallAwake()        {                                LuaFunction fun = table.GetLuaFunction("Awake");            if (fun != null)                fun.Call(table, gameObject);        }        void Start()        {            LuaFunction fun = table.GetLuaFunction("Start");            if (fun != null)                fun.Call(table, gameObject);        }        void Update()        {            //效率问题有待测试和优化            //可在lua中调用UpdateBeat替代            LuaFunction fun = table.GetLuaFunction("Update");            if (fun != null)                fun.Call(table, gameObject);        }    }}

里面的AddButtonClick, Text, Add 三个方法,我们会在之后使用。


然后制作如下的简单UI界面和cube


并且把它们放到如图所示的文件夹内,制作成prefab。


然后找到 Packager.cs 脚本,添加如下代码:



然后我们来写 myTestLua.lua 的代码,如下:

 myTestLua = { name = "myTestLua"}local golocal cubefunction myTestLua:Awake()print("Awake  + "..self.name)LuaHelper = LuaFramework.LuaHelper;resMgr = LuaHelper.GetResManager();resMgr:LoadPrefab('mytestpanel', {'myTestPanel', 'Cube'}, OnFinishLoad)resMgr:LoadPrefab('cube', {'Cube'}, OnCubeLoadFinish)print("Awake  + "..self.name)endfunction OnFinishLoad(objs)go = UnityEngine.GameObject.Instantiate(objs[0]);local parent = UnityEngine.GameObject.Find("Canvas");go.transform.parent = parent.transform;go.transform.localScale = Vector3.one;go.transform.localPosition = Vector3.zero;local btn = go.transform:FindChild("Button").gameObject;LuaFramework.TestComponemt.AddButtonClick(btn, OnClick);endfunction OnCubeLoadFinish(objs)cube = UnityEngine.GameObject.Instantiate(objs[0])print("load finish")UpdateBeat:Add(Update, cube)endfunction Update()local Input = UnityEngine.Inputlocal h = Input.GetAxis("Horizontal")local v = Input.GetAxis("Vertical")local x = cube.transform.position.x + hlocal y = cube.transform.position.y + vcube.transform.position = Vector3.New(x, y, 0)endfunction OnClick()local text = go.transform:FindChild("Text").gameObject;print("can change text?")text:GetComponent(typeof(UnityEngine.UI.Text)).text = "Hello, LuaFramework";--LuaFramework.TestComponemt.Text(text, "Hello, LuaFramework");print("ok?")endfunction myTestLua:Update()endfunction myTestLua:New(obj)local o = {}setmetatable(o, self)self.__index = selfreturn oendfunction testPrint()print("hello, LuaFramework  "..self.name)endreturn myTestLua


最后,运行程序,点击button按钮,点击WASD键,程序运行正常。




如有不懂,欢迎评论

0 0
原创粉丝点击