热更学习笔记10~11----lua调用C#中的List和Dictionary、拓展类中的方法
1.Unity学习笔记--基础2.Unity学习笔记--入门3.Unity学习笔记--数据持久化之PlayerPrefs的使用4.Unity学习笔记--数据持久化XML文件(1)5.Unity学习笔记--数据持久化XML文件(2)6.Unity学习笔记--数据持久化Json7.NGUI学习笔记(1)8.NGUI学习笔记29.NGUI学习笔记3.510.NGUI学习笔记4.011.Unity 热更--AssetBundle学习笔记 0.712.Unity 热更--AssetBundle学习笔记 0.813.Unity 热更--AssetBundle学习笔记 1.0【AB包资源加载工具类的实现】14.[2]自定义Lua解析方式15.Unity热更学习toLua使用--[1]toLua的导入和默认加载执行lua脚本16.自定义Lua解析器管理器-------演化脚本V0.517.使用自定义委托来调用Lua中的多返回值和长参数类型函数18.使用自定义lua解析管理器调用lua脚本中的table19.Lua热更学习--使用toLua中的协程20.toLua中Lua调用C#中的类
21.热更学习笔记10~11----lua调用C#中的List和Dictionary、拓展类中的方法
22.Lua中调用ref和out修饰参数的函数/重载函数23.Unity 编辑器中获取选中的文件夹、文件路径[10]Lua脚本调用C#中的List和Dictionary
调用还是在上文中使用的C#脚本中Student类:
lua脚本:
print("------------访问使用C#脚本中的List和Dictionary-----------") student.list:Add(2024) student.list:Add(5) student.list:Add(18) local listSize = student.list.Count print("list长度".. tostring(listSize)) --遍历list for i = 0,listSize - 1 do print("--->" .. student.list[i]) end print(student.list) student.dic:Add(1,"北京") student.dic:Add(2,"上海") student.dic:Add(3,"广州") student.dic:Add(4,"深圳") --遍历dic --使用迭代器遍历键值对 local iterDic = student.dic:GetEnumerator() while iterDic:MoveNext() do local val = iterDic.Current.Value print("--->" .. iterDic.Current.Key .. "---".. val) end --单独遍历键 local iterDicKey = student.dic.Keys:GetEnumerator() while iterDicKey:MoveNext() do print("键---" .. iterDicKey.Current) end --遍历值 local iterDicVal = student.dic.Values:GetEnumerator() while iterDicVal:MoveNext() do print("值---" .. iterDicVal.Current) end ----------------------在lua脚本中新增C#中的dic和list ----比较麻烦 需要新添加要增加的数据结构类型 --新增list local list2 = System.Collections.Generic.List_int() list2:Add(5) list2:Add(16) print("list2[0]" .. list2[0]) print("list2[1]" .. list2[1]) --新增加字典 local dic2 = System.Collections.Generic.Dictionary_int_string() dic2:Add(521,"我爱你") local iterDic2 = dic2:GetEnumerator() while iterDic2:MoveNext() do local key = iterDic2.Current.Key local val = iterDic2.Current.Value print("Dic2---" .. key .. "--" .. val) end
CustomSetting中新增的使用的C#中的数据结构类型,就此可以体会一下lua中的userdata的数据类型!
[11]Lua中调用C#类中的拓展方法
在lua中调用Student类的拓展方法
--调用拓展类中的方法 local Jack = TestScripts.Student("Jack") --调用拓展方法 . 调用需要传一个自身引用 Jack.SingSunny(self) --或者 使用 :调用 Jack:SingSunny()
所调用的C#脚本中的类:
public static class Tools { public static void SingSunny(this Student student) { Debug.Log("我在唱周董的晴天!"); } } public class Student { private string _name; public Student(string name) { _name = name; } public void Speak() { Debug.Log("my name is " + _name); } //+ 新增数组内容 public int[] array = new int[5] { 1, 2, 3, 4, 5 }; public List<int> list = new List<int>() { 6, 7, 8, 9, 10 }; public Dictionary<int, string> dic = new Dictionary<int, string>(); }
CustomSetting中将拓展类Tools和被拓展的类Student相关联:
调用结果打印:
本文作者:畅知
本文链接:https://www.cnblogs.com/TonyCode/p/18199897
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步