unity3d开发教程-用lua实现组件 unity lua ui框架
liebian365 2024-11-14 18:02 16 浏览 0 评论
一、Lua组件基类
1、在 Assets/Lua 目录下新建Com目录用于存放Lua组件
2、在Assets/Lua/Com 目录新建Component.lua文件,添加Lua组件基类Component,实现Unity3D组件的生命周期
Assets/Lua/Com/Component.lua
1 ---@class Component @Component class
2 local Component = {}
3
4 --- Awake
5 function Component:Awake()
6 end
7
8 --- OnEnable
9 function Component:OnEnable()
10 end
11
12 --- Start
13 function Component:Start()
14 end
15
16 --- Update
17 function Component:Update()
18 end
19
20 --- FixedUpdate
21 function Component:FixedUpdate()
22 end
23
24 --- LateUpdate
25 function Component:LateUpdate()
26 end
27
28 --- OnGUI
29 function Component:OnGUI()
30 end
31
32 --- OnDisable
33 function Component:OnDisable()
34 end
35
36 --- OnDestroy
37 function Component:OnDestroy()
38 end
39
40 --- ExtendComponent
41 ---@return Component
42 function ExtendComponent()
43 return CreateComponent(Component)
44 end
45
46 --- CreateComponent
47 ---@param componentClass Component
48 ---@return Component
49 function CreateComponent(componentClass)
50 local o = {}
51
52 for k, v in pairs(componentClass) do
53 o[k] = v
54 end
55
56 return o
57 end
58
59 local com = {
60 ExtendComponent = ExtendComponent,
61 CreateComponent = CreateComponent,
62 }
63
64 return com
3、基类 Component 只是实现了空的生命周期方法,子类只需要实现所需的生命周期方法,子类没有实现的生命周期方法会有默认的空实现。
4、由于通过C#调用Lua的方法时,以元表方式继承的方法会报空。这里通过拷贝key方式继承基类,调用Com.ExtendComponent()方法返回一个继承Component的子类table。
5、子类通过 Com.CreateComponent 方法创建对象。
6、下面给出一个简单的样例组件 TestComponent
Assets/Lua/Com/TestComponent.lua
1 local Com = require("Assets.Lua.Com.Component")
2
3 ---@class TestComponent @TestComponent class
4 TestComponent = Com.ExtendComponent()
5
6 function TestComponent.new(paramList)
7 local o = Com.CreateComponent(TestComponent)
8
9 -- member fields
10 o.num = paramList[0] -- Array by C#, index begin from 0
11 return o
12 end
13
14 function TestComponent:Awake()
15 print(self.num)
16 print("TestComponent:Awake")
17 end
18
19 function TestComponent:Start()
20 print("TestComponent:Start")
21 end
22
23 function TestComponent:OnDestroy()
24 print("TestComponent:OnDestroy")
25 end
注意:
1) 这里 TestComponent 是全局的变量,因为C#直接访问的是全局变量,局部变量无法直接访问。
2)C#传过来的数组参数 paramList,下标从0开始
二、通用C#组件脚本
1 using UnityEngine;
2 using LuaInterface;
3
4 public class LuaComponent : MonoBehaviour
5 {
6 public string luaClassName = "";
7 public string[] paramList = null;
8
9 private LuaState luaState = null;
10 private LuaTable luaObj = null;
11
12 void Awake()
13 {
14 LuaClient luaClient = LuaClient.Instance;
15 this.luaState = luaClient.GetLooper().luaState;
16 this.luaState.DoFile(this.luaClassName + ".lua");
17 this.luaObj = callLuaNew();
18
19 callLuaFunc("Awake");
20 }
21
22 void OnEnable()
23 {
24 callLuaFunc("OnEnable");
25 }
26
27 // Start is called before the first frame update
28 void Start()
29 {
30 callLuaFunc("Start");
31 }
32
33 // Update is called once per frame
34 void Update()
35 {
36 callLuaFunc("Update");
37 }
38
39 void FixedUpdate()
40 {
41 callLuaFunc("FixedUpdate");
42 }
43
44 void LateUpdate()
45 {
46 callLuaFunc("LateUpdate");
47 }
48
49 void OnGUI()
50 {
51 callLuaFunc("OnGUI");
52 }
53
54 void OnDisable()
55 {
56 if (LuaClient.Instance != null)
57 {
58 callLuaFunc("OnDisable");
59 }
60 }
61
62 void OnDestroy()
63 {
64 if (LuaClient.Instance != null)
65 {
66 callLuaFunc("OnDestroy");
67 }
68
69 this.luaState = null;
70 this.luaObj = null;
71 }
72
73 public LuaTable callLuaNew()
74 {
75 LuaFunction luaFunc = luaState.GetFunction(this.luaClassName + "." + "new");
76 luaFunc.BeginPCall();
77 luaFunc.Push(this.paramList);
78 luaFunc.PCall();
79 LuaTable table = luaFunc.CheckLuaTable();
80 luaFunc.EndPCall();
81 luaFunc.Dispose();
82 luaFunc = null;
83 return table;
84 }
85
86 private void callLuaFunc(string funcName)
87 {
88 LuaFunction luaFunc = luaState.GetFunction(this.luaClassName + "." + funcName);
89 luaFunc.BeginPCall();
90 luaFunc.Push(this.luaObj);
91 luaFunc.PCall();
92 luaFunc.EndPCall();
93 luaFunc.Dispose();
94 luaFunc = null;
95 }
96 }
1、这个组件给 Unity3D暴露两个字段:luaClassName、paramList。可以在Unity3D中设置Lua类名、初始化参数列表(参数个数可在Unity3D调整)
2、在 Awake 生命周期加载跟 luaClassName 同名的 .lua 文件,调用 Lua的 luaClassName 类的 new 方法,把 paramList 当作参数传过去,得到 Lua对象。
3、其他生命周期方法都是调用 Lua 类的同名方法,从而可以在 Lua 实现具体的生命周期逻辑。
三、添加 Lua 组件搜索路径
为了让 Lua虚拟机知道组件的路径,在Main.cs重写父类的 Awake 生命周期方法,添加搜索路径 Lua/Com
Assets/CSharp/Main.cs
1 new void Awake()
2 {
3 base.Awake();
4
5 // search path
6 string fullPath = Application.dataPath + "/Lua/Com";
7 luaState.AddSearchPath(fullPath);
8 }
四、测试效果
1、在GameObject菜单,选择Create Empty,添加一个空GameObject
2、在属性面板给这个空GameObject添加Lua Component组件,设置 Lua Class Name 为 TestComponent,下标0的参数为 12
3、运行效果日志
相关推荐
- 4万多吨豪华游轮遇险 竟是因为这个原因……
-
(观察者网讯)4.7万吨豪华游轮搁浅,竟是因为油量太低?据观察者网此前报道,挪威游轮“维京天空”号上周六(23日)在挪威近海发生引擎故障搁浅。船上载有1300多人,其中28人受伤住院。经过数天的调...
- “菜鸟黑客”必用兵器之“渗透测试篇二”
-
"菜鸟黑客"必用兵器之"渗透测试篇二"上篇文章主要针对伙伴们对"渗透测试"应该如何学习?"渗透测试"的基本流程?本篇文章继续上次的分享,接着介绍一下黑客们常用的渗透测试工具有哪些?以及用实验环境让大家...
- 科幻春晚丨《震动羽翼说“Hello”》两万年星间飞行,探测器对地球的最终告白
-
作者|藤井太洋译者|祝力新【编者按】2021年科幻春晚的最后一篇小说,来自大家喜爱的日本科幻作家藤井太洋。小说将视角放在一颗太空探测器上,延续了他一贯的浪漫风格。...
- 麦子陪你做作业(二):KEGG通路数据库的正确打开姿势
-
作者:麦子KEGG是通路数据库中最庞大的,涵盖基因组网络信息,主要注释基因的功能和调控关系。当我们选到了合适的候选分子,单变量研究也已做完,接着研究机制的时便可使用到它。你需要了解你的分子目前已有哪些...
- 知存科技王绍迪:突破存储墙瓶颈,详解存算一体架构优势
-
智东西(公众号:zhidxcom)编辑|韦世玮智东西6月5日消息,近日,在落幕不久的GTIC2021嵌入式AI创新峰会上,知存科技CEO王绍迪博士以《存算一体AI芯片:AIoT设备的算力新选择》...
- 每日新闻播报(September 14)_每日新闻播报英文
-
AnOscarstatuestandscoveredwithplasticduringpreparationsleadinguptothe87thAcademyAward...
- 香港新巴城巴开放实时到站数据 供科技界研发使用
-
中新网3月22日电据香港《明报》报道,香港特区政府致力推动智慧城市,鼓励公私营机构开放数据,以便科技界研发使用。香港运输署21日与新巴及城巴(两巴)公司签署谅解备忘录,两巴将于2019年第3季度,开...
- 5款不容错过的APP: Red Bull Alert,Flipagram,WifiMapper
-
本周有不少非常出色的app推出,鸵鸟电台做了一个小合集。亮相本周榜单的有WifiMapper's安卓版的app,其中包含了RedBull的一款新型闹钟,还有一款可爱的怪物主题益智游戏。一起来看看我...
- Qt动画效果展示_qt显示图片
-
今天在这篇博文中,主要实践Qt动画,做一个实例来讲解Qt动画使用,其界面如下图所示(由于没有录制为gif动画图片,所以请各位下载查看效果):该程序使用应用程序单窗口,主窗口继承于QMainWindow...
- 如何从0到1设计实现一门自己的脚本语言
-
作者:dong...
- 三年级语文上册 仿写句子 需要的直接下载打印吧
-
描写秋天的好句好段1.秋天来了,山野变成了美丽的图画。苹果露出红红的脸庞,梨树挂起金黄的灯笼,高粱举起了燃烧的火把。大雁在天空一会儿写“人”字,一会儿写“一”字。2.花园里,菊花争奇斗艳,红的似火,粉...
- C++|那些一看就很简洁、优雅、经典的小代码段
-
目录0等概率随机洗牌:1大小写转换2字符串复制...
- 二年级上册语文必考句子仿写,家长打印,孩子照着练
-
二年级上册语文必考句子仿写,家长打印,孩子照着练。具体如下:...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- wireshark怎么抓包 (75)
- qt sleep (64)
- cs1.6指令代码大全 (55)
- factory-method (60)
- sqlite3_bind_blob (52)
- hibernate update (63)
- c++ base64 (70)
- nc 命令 (52)
- wm_close (51)
- epollin (51)
- sqlca.sqlcode (57)
- lua ipairs (60)
- tv_usec (64)
- 命令行进入文件夹 (53)
- postgresql array (57)
- statfs函数 (57)
- .project文件 (54)
- lua require (56)
- for_each (67)
- c#工厂模式 (57)
- wxsqlite3 (66)
- dmesg -c (58)
- fopen参数 (53)
- tar -zxvf -c (55)
- 速递查询 (52)