热更学习笔记--toLau中lua脚本对C#中枚举和数组的访问
[8]Lua脚本调用C#中的枚举学习
--调用枚举类型 print("----------------------toLua中调用C#中枚举类型-------------------------") PrimitiveType = UnityEngine.PrimitiveType local cubeObj = GameObject.CreatePrimitive(PrimitiveType.Cube) --使用自定义枚举 local tc = TestScripts.OUTSTANDING_STUDENTS.TonyChang print("优秀学生代表----" .. tostring(tc) .. "排名" .. tc:ToInt()) local fl = TestScripts.OUTSTANDING_STUDENTS.IntToEnum(2) print("数值转枚举" .. tostring(fl)) print("枚举值一样二者可以判等吗?") local us = TestScripts.OUTSTANDING_STUDENTS.UrusWong local us2 = TestScripts.OUTSTANDING_STUDENTS.UrusWong if us == us2 then print("一样") else print("不一样") end
调用的C#脚本新增内容
#region 枚举 //在TestScripts命名空间下新增枚举类型 public enum OUTSTANDING_STUDENTS { TonyChang, UrusWong, FrankLee, JackChow } #endregion
运行结果
[9]Lua脚本中调用C#中的数组
调用的C#脚本新增内容:
Lua脚本中访问数组:
因为Array数组是在前文中的测试脚本TestScripts.Student类中声明创建的,所以开头先实例化Student对象。
使用Lua访问C#中数组时,可以通过层级结构获取到对应类的数组索引,按照C#中array数据结构类型来访问,
注意索引最大值,可以通过迭代器遍历访问,也可以将其转换为lua中的table,再遍历访问。
最后我们尝试再Lua中使用C#的array数组结构来创建使用数组
local student = TestScripts.Student("tony") print("----------------------访问C#中数组--------------------") print("array的长度" .. student.array.Length) print("array[0]=".. tostring(student.array[0])) print("5 在array中的索引是" .. tostring(student.array:IndexOf(5))) --按照C#中array数组长度进行遍历访问 --所以长度要减一 才为最后一个索引 for i = 1, student.array.Length - 1 do print("array[" .. i .. "] = " .. student.array[i]) end --使用迭代器遍历 local iter = student.array:GetEnumerator() while iter:MoveNext() do print("iter:" .. iter.Current) end --转成table再遍历 local aTable = student.array:ToTable() for j=1,#aTable do print("aTable: " .. tostring(aTable[j])) end --在lua中使用C#中的Array创建数组 --奇葩 local createArray = System.Array.CreateInstance(typeof(System.Int32), 10) print("createArray.Length = " .. createArray.Length) local createArray = System.Array.CreateInstance(typeof(System.Int32), 10) createArray[0] = 9999 print("createArray[0] = " .. createArray[0]) print("createArray" .. createArray)
运行结果:
注意:本两次在CustomSetting中新增的类型,之后重新生成wrap文件,在lua中才可以正常使用到。总之,使用toLua获取C#脚本中内容,则需要将脚本添加自定文件,并生成wrap文件。
本文作者:畅知
本文链接:https://www.cnblogs.com/TonyCode/p/18195090
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Unity学习笔记
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步