热更学习笔记--toLau中lua脚本对C#中枚举和数组的访问
  1wMBnbixARwC 2024年05月17日 50 0

[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

运行结果

image-20240515230826214

[9]Lua脚本中调用C#中的数组

调用的C#脚本新增内容:

image-20240515235259197

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)

运行结果:

image-20240515235136442

注意:本两次在CustomSetting中新增的类型,之后重新生成wrap文件,在lua中才可以正常使用到。总之,使用toLua获取C#脚本中内容,则需要将脚本添加自定文件,并生成wrap文件。

img

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年05月17日 0

暂无评论

推荐阅读
  oXKBKZoQY2lx   2024年05月17日   54   0   0 游戏开发
  1wMBnbixARwC   2024年05月17日   50   0   0 游戏开发
  1wMBnbixARwC   2024年05月08日   77   0   0 游戏开发
1wMBnbixARwC