【ulua入门】(2) 动态实例化GameObject,然后更改UGUI元素的值,使用案件移动物体
来源:互联网 发布:白敬亭 演技 知乎 编辑:程序博客网 时间:2024/05/21 06:51
参考链接 : http://www.manew.com/thread-91203-1-1.html
这是第二篇关于ulua的博客,关于基础的部分,请看第一篇。
里面的AddButtonClick, Text, Add 三个方法,我们会在之后使用。
在这里下载ulua : https://github.com/jarjin/LuaFramework_UGUI
首先,我们新建一个 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
- 【ulua入门】(2) 动态实例化GameObject,然后更改UGUI元素的值,使用案件移动物体
- <Unity3D>实例化UGUI中GameObject 并设置它的位置
- 【ulua入门】(1) 让lua跑起来,然后利用框架,写自己的逻辑
- UGUI的Slider与Gameobject的结合使用
- 【Unity&UGUI】动态改变精灵物体的图片
- Unity 3D ULua 基于UGUI 初次使用的心得与体会
- UGUI 实现物体的双击
- unity中使用SetActive()和gameobject实例化的陷坑总结
- 关于UGUI的实例化
- 移动GameObject
- uLua实例
- U3D关于物体移动的方法的实例
- UGUI 滚动优化之 隐藏视口外的GameObject
- [转]EventSystem在UGUI上的使用和EventSystem在非UGUI游戏物体上的使用
- 【Unity】获得当前脚本的物体的transform和gameobject
- Unity使用Shader动态的剪切物体
- Unity中GameObject对子物体的树形结构管理
- [Unity&对象]如何把大量GameObject 物体 快速的放入 一个 GameObject 数组中
- 契约——XML + XSL
- (4)Dojo学习之动画学习
- #pragma命令详解
- Java8 中使用forEach + lambda expression/method reference 循环List和Map
- Week1 线性表的学习
- 【ulua入门】(2) 动态实例化GameObject,然后更改UGUI元素的值,使用案件移动物体
- docker1.12 swarm 自定义镜像的启动
- Android-25种开源炫酷动画框架
- 利用UpdatePanel+Timer 控件自动更新Gridview的数据
- 如何写一个内存溢出的程序
- 关于使用腾讯云播放器的遇到的坑
- Android volley 解析(三)之文件上传篇
- 移动端line-height问题
- tomcat 配置多个虚拟域名访问