--1.添加元素 obj.dic:Add(1,"123") print(obj.dic[1]) --2.遍历 for key, value inpairs(obj.dic) do print(key.. ""..value) end --3.Lua中创建字典 --这里相当于得到了对应类型 local dic2_String_Vector3 = CS.System.Collections.Generic.Dictionary(CS.System.String,CS.UnityEngine.Vector3) local dic2 = dic2_String_Vector3() dic2:Add("123",CS.UnityEngine.Vector3.right) for key, value inpairs(dic2) do print(key,value) end --注意:在Lua中创建的字典 直接通过中括号 得到的时 nil print(dic2["123"]) --如果要在Lua中创建的字典 得到对应的值 只能通过 get_Item方法 print(dic2:get_Item("123")) --如果要在Lua中创建的字典 改变对应的值 只能通过 set_Item方法 dic2:set_Item("123",nil) print(dic2:get_Item("123"))
publicclassLesson5 { publicintRefFun(int a, refint b, refint c, int d) { b = a + d; c = a - d; return100; } publicintOutFun(int a, outint b, outint c, int d) { b = a; c = d; return200; } publicintRefOutFun(int a, outint b, refint c) { b = a * 10; c = a * 20; return300; } }
publicclassLesson6 { publicintCalc() { return100; } publicintCalc(int a, int b) { return a + b; } publicintCalc(int a) { return a; } publicfloatCalc(float a) { return a; }
--得到函数的指定信息 local m1 = typeof(CS.Lesson6):GetMethod("Calc",{typeof(CS.System.Int32)}) local m2 = typeof(CS.Lesson6):GetMethod("Calc",{typeof(CS.System.Single)})
--通过xlua提供的方法 把他转成lua函数来使用 --一般我们转一次之后 反复使用 local f1 = xlua.tofunction(m1) local f2 = xlua.tofunction(m2) --成员方法:第一个参数 为 调用对象 --静态方法:不用传对象 print(f1(obj,10)) print(f2(obj,10.2))
七、委托和事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassLesson7 { public UnityAction del; publicevent UnityAction eventAction;
local obj = GameObject("测试脚本") --获取身上的刚体组件 如果没有就加 有就不管 local rig = obj:GetComponent(typeof(Rigidbody)) --注意在lua中nil和null无法 == 比较 -- 所以我们通过Equals方法进行比较 --但对象本来就是nil,就会出问题 --综上:一般我们会自己写一个全局方法来判空 functionIsNull(obj) if obj==nilor obj:Equals(nil) then returntrue end returnfalse end if rig:Equals(nil) then rig = obj:AddComponent(typeof(Rigidbody)) end if IsNull(rig) then rig = obj:AddComponent(typeof(Rigidbody)) end
GameObject = CS.UnityEngine.GameObject WaitForSeconds =CS.UnityEngine.WaitForSeconds --在场景中新建一个空物体 然后挂载一个脚本去 继承mono使用它来开启协程 local obj = GameObject("Coroutine") local mono = obj:AddComponent(typeof(CS.LuaCallCS))
fun = function() local a = 1 whiletruedo --lua中不能直接使用 c#中的yield reture --就使用lua中的协程返回 coroutine.yield(WaitForSeconds(1)) print(a) a = a + 1 if a>10then mono:StopCoroutine(b) end end end --注意: --我们不能将lua函数 传入到C#中的开启协程函数中
--使用C#中的开启协程函数,必须先调用xlua中的util.cs_generator b = mono:StartCoroutine(util.cs_generator(fun))