GameObject = UnityEngine.GameObject
local obj =GameObject("zzs")print(GameObject.Find("zzs").name);print(obj.transform.position.x)
Vector3 = UnityEngine.Vector3
obj.transform:Translate(Vector3(1,1,1))
obj:AddComponent(typeof(LuaCallCSharp))local test =Test()
test:Speek("zzs")
二.ToLua调用C#枚举
规则与Xlua相同,和上面的类一样,都需要添加到CustomSetting中的列表中,重新生成
publicenumState{
Idle,
Run,
Atk
}
PrimitiveType = UnityEngine.PrimitiveType
GameObject = UnityEngine.GameObject
GameObject.CreatePrimitive(PrimitiveType.Capsule)local a = State.Idle
local b = State.Run
local c = State.Atk
print(a)print(b)print(c)if a==b thenprint("相等")elseprint("不相等")end--枚举转字符串print(tostring(a))--枚举转Intprint(a:ToInt())print(b:ToInt())--Int转枚举print(tostring(State.IntToEnum(2)))
三.ToLua中调用C#中数组
与Xlua相比多了一种将数组转换成Table的方法
publicclassTest2{publicint[] arr ={1,2,3,4,6};}
local test2 =Test2()print(test2.arr.Length)print(test2.arr[0])--第一种遍历for i =0, test2.arr.Length -1doprint(test2.arr[i])endprint("--------------------")--第二种遍历local t = test2.arr:ToTable()for i =1,#t doprint(t[i])end--创建数组local arr = System.Array.CreateInstance(typeof(System.Int32),3)print(arr.Length)
arr[0]=10
arr[1]=20
arr[2]=30for i =0, arr.Length -1doprint(arr[i])end