Unity3D开发入门教程(四)——用Lua实现组件

2023-11-13

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475
五邑隐侠,本名关健昌,12年游戏生涯。 本教程以 Unity 3D + VS Code + C# + tolua 为例。

一、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、运行效果日志

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity3D开发入门教程(四)——用Lua实现组件 的相关文章

  • Python 3 os.urandom

    在哪里可以找到完整的教程或文档os urandom 我需要获得一个随机 int 来从 80 个字符的字符串中选择一个字符 如果你只需要一个随机整数 你可以使用random randint a b 来自随机模块 http docs pytho
  • 如何在python 3.7中生成条形码

    我正在使用 python 3 7 为了生成条形码 我尝试使用安装 pyBarcode 库pip install pyBarcode 但它显示以下错误 找不到满足 pyBarcode 要求的版本 来自版本 找不到 pyBarcode 的匹配分
  • python 中的并行处理

    在 python 2 7 中进行并行处理的简单代码是什么 我在网上找到的所有示例都很复杂 并且包含不必要的代码 我该如何做一个简单的强力整数分解程序 在每个核心 4 上分解 1 个整数 我真正的程序可能只需要2个核心 并且需要共享信息 我知
  • 为什么删除临时文件时出现WindowsError?

    我创建了一个临时文件 向创建的文件添加了一些数据 已保存 然后尝试将其删除 但我越来越WindowsError 编辑后我已关闭该文件 如何检查哪个其他进程正在访问该文件 C Documents and Settings Administra
  • 如何检查python xlrd库中的excel文件是否有效

    有什么办法与xlrd库来检查您使用的文件是否是有效的 Excel 文件 我知道还有其他库可以检查文件头 我可以使用文件扩展名检查 但为了多平台性我想知道是否有任何我可以使用的功能xlrd库本身在尝试打开文件时可能会返回类似 false 的内
  • Kivy - 有所有颜色名称的列表吗?

    在 Kivy 中 小部件 color属性允许输入其值作为字符串颜色名称 也 例如在 kv file Label color red 是否有所有可能的颜色名称的列表 就在这里 来自Kivy 的文档 https kivy org doc sta
  • Python - 来自 .进口

    我第一次尝试图书馆 我注意到解决图书馆内导入问题的最简单方法是使用如下结构 from import x from some module import y 我觉得这件事有些 糟糕 也许只是因为我不记得经常看到它 尽管公平地说我还没有深入研究
  • Python 2.7 中的断言对我来说不起作用示例assertIn

    我的 Mac 上安装了 python 2 7 通过在终端中运行 python v 进行验证 当我尝试使用任何新的 2 7 断言方法时 我收到 AtributeError 我看过http docs python org 2 library u
  • pyspark 数据框中的自定义排序

    是否有推荐的方法在 pyspark 中实现分类数据的自定义排序 我理想地寻找 pandas 分类数据类型提供的功能 因此 给定一个数据集Speed列 可能的选项是 Super Fast Fast Medium Slow 我想实现适合上下文的
  • 工作日重新订购 Pandas 系列

    使用 Pandas 我提取了一个 CSV 文件 然后创建了一系列数据来找出一周中哪几天崩溃最多 crashes by day bc DAY OF WEEK value counts 然后我将其绘制出来 但当然它按照与该系列相同的排名顺序绘制
  • Python3.0 - 标记化和取消标记化

    我正在使用类似于以下简化脚本的内容来解析较大文件中的 python 片段 import io import tokenize src foo bar src bytes src encode src io BytesIO src src l
  • 搜索多个字段

    我想我没有正确理解 django haystack 我有一个包含多个字段的数据模型 我希望搜索其中两个字段 class UserProfile models Model user models ForeignKey User unique
  • 如何在 React Native 中渲染自定义 3D 对象

    我已经成功使用 Three js expo Three 和 expo gl 在 React Native 中配置了红色立方体的 3D 渲染 但我想让用户渲染他们自己可能拥有的自定义 3D 对象 obj 或 mtl 扩展名 但我不确定如何让他
  • Matplotlib 中 x 轴标签的频率和旋转

    我在下面编写了一个简单的脚本来使用 matplotlib 生成图形 我想将 x tick 频率从每月增加到每周并轮换标签 我不知道从哪里开始 x 轴频率 我的旋转线产生错误 TypeError set xticks got an unexp
  • 用 python 编写的数学语法检查器

    我需要的只是使用 python 检查字符串是否是有效的数学表达式 为了简单起见 假设我只需要 运算符 也作为一元 带有数字和嵌套括号 为了完整性 我还添加了简单的变量名称 所以我可以这样测试 test 3 2 1 valid test 3
  • 返回表示每组内最大值的索引的一系列数字位置

    考虑一下这个系列 np random seed 3 1415 s pd Series np random rand 100 pd MultiIndex from product list ABDCE list abcde One Two T
  • Mac OSX 10.6 上的 Python mysqldb 不工作

    我正在使用 Python 2 7 并尝试让 Django 项目在 MySQL 后端运行 我已经下载了 mysqldb 并按照此处的指南进行操作 http cd34 com blog programming python mysql pyth
  • Elasticsearch 通过搜索返回拼音标记

    我用语音分析插件 https www elastic co guide en elasticsearch plugins current analysis phonetic html由于语音转换 从弹性搜索中进行一些字符串匹配 我的问题是
  • Django 与谷歌图表

    我试图让谷歌图表显示在我的页面上 但我不知道如何将值从 django 视图传递到 javascript 以便我可以绘制图表 姜戈代码 array Year Sales Expenses 2004 1000 400 2005 1170 460
  • 如何为不同操作系统/Python 版本编译 Python C/C++ 扩展?

    我注意到一些成熟的Python库已经为大多数架构 Win32 Win amd64 MacOS 和Python版本提供了预编译版本 针对不同环境交叉编译扩展的标准方法是什么 葡萄酒 虚拟机 众包 我们使用虚拟机和Hudson http hud

随机推荐