- 再Lua中没有关键字,所有CSharp相关的都在CS下。
访问构造方法,静态变量,方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| local gameobject1 = CS.UnityEngine.GameObject()
print(gameobject1.name)
gameobject1.name = "GameObject1"
gameobject1:SetActive(false)
local gameobject2 = CS.UnityEngine.GameObject("GameObject2")
CS.UnityEngine.Object.DontDestroyOnLoad(gameobject2)
|
访问重载方法
由于lua中对于所有数字都是’number’类型,所以lua只支持有限的重载,如参数个数不同。
1 2 3 4 5 6 7 8 9 10 11
| public class Animal { public void SayHello() { UnityEngine.Debug.Log("Hello"); } public void SayHello(string name) { UnityEngine.Debug.Log("Hello " + name); } }
|
1 2 3
| local dog = CS.Animal() dog:SayHello() dog:SayHello("Cat")
|
枚举
1 2 3 4
| public enum Direction { Left,Right,Buttom,Top, }
|
1 2 3 4 5 6 7 8 9 10 11 12
| print(tostring(CS.Direction.Left))
print(tostring(CS.Direction.__CastFrom(1)))
print(tostring(CS.Direction.__CastFrom('Buttom')))
|
访问委托
复杂类型与表的转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class A { public int num1; public int num2; } class B { public A a; public int num3;
public int Add(B b) { return b.a.num1 + b.a.num2 + b.num3; } }
|
1 2 3
| local _b = CS.B() local result = _b:Add({a = {num1=11, num2 = 12}, num3 = 13}) print(result)
|