01.new C#对象

你在C#这样new一个对象:
var newGameObj = new UnityEngine.GameObject();
对应到Lua是这样:
local newGameObj = CS.UnityEngine.GameObject()
基本类似,除了:
1、lua里头没有new关键字;
2、所有C#相关的都放到CS下,包括构造函数,静态成员属性、方法;
如果有多个构造函数呢?放心,xlua支持重载,比如你要调用GameObject的带一个string参数的构造函数,这么写:
local newGameObj2 = CS.UnityEngine.GameObject(‘helloworld’)

local newGo1= CS.UnityEngine.GameObject("newGo1");
local newGo2= CS.UnityEngine.GameObject("newGo2");

--访问对象属性
print("New对象名字:"..newGo1.name..":"..newGo2.name);
--修改对象属性
newGo1.name="Go1";
newGo2.name="Go2";
print("New对象修改后的名字:"..newGo1.name..":"..newGo2.name);

--调用对象方法(调用对象方法时注意使用":"语法糖)
newGo1:SetActive(false);
newGo2.transform.position= {x = 10, y = 20, z = 30};

07.XLua Lua访问C#中的方法_lua



访问C#静态属性,方法
读静态属性
CS.UnityEngine.Time.deltaTime
写静态属性
CS.UnityEngine.Time.timeScale = 0.5
调用静态方法
CS.UnityEngine.GameObject.Find(‘helloworld’)
小技巧:如果需要经常访问的类,可以先用局部变量引用后访问,除了减少敲代码的时间,还能提高性能:
local GameObject = CS.UnityEngine.GameObject
GameObject.Find(‘helloworld’)

--读取静态属性
local time=CS.UnityEngine.Time;
print(time.timeScale);
--调用静态方法(调用静态方法时可以直接使用".")
local go=CS.UnityEngine.GameObject;
local transform=go.Find("Value").transform;
transform.position={x = 1, y = 2, z = 3};

07.XLua Lua访问C#中的方法_静态属性_02


03.访问C#成员属性,方法

如果要通过Lua访问自己定义的类的话,需要给被Lua代码访问的类加上一个Attribute:[LuaCallCSharp],用来生成Lua的适配代码

下面对[LuaCallCSharp]的解释参考xLua的Github主页的FAQ

XLua.LuaCallCSharp

一个C#类型加了这个配置,xLua会生成这个类型的适配代码(包括构造该类型实例,访问其成员属性、方法,静态属性、方法),否则将会尝试用性能较低的反射方式来访问。

一个类型的扩展方法(Extension Methods)加了这配置,也会生成适配代码并追加到被扩展类型的成员方法上。

xLua只会生成加了该配置的类型,不会自动生成其父类的适配代码,当访问子类对象的父类方法,如果该父类加了LuaCallCSharp配置,则执行父类的适配代码,否则会尝试用反射来访问。

反射访问除了性能不佳之外,在il2cpp下还有可能因为代码剪裁而导致无法访问,后者可以通过下面介绍的ReflectionUse标签来避免。

XLua.ReflectionUse

一个C#类型类型加了这个配置,xLua会生成link.xml阻止il2cpp的代码剪裁。

对于扩展方法,必须加上LuaCallCSharp或者ReflectionUse才可以被访问到。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

[LuaCallCSharp]
public class BaseClass
{
public string BaseField="BaseField";
public string _BaseProperty="BaseProperty";
public string BaseProperty
{
set { _BaseProperty = value; }
get { return _BaseProperty; }
}

public static string BaseStatic = "BaseStatic";
public static string _BaseStaticProperty = "BaseStaticProperty";
public static string BaseStaticProperty
{
set { _BaseStaticProperty = value; }
get { return _BaseStaticProperty; }
}

public void BasePrint()
{
Debug.Log("BasePrint() Called.");
}
public static void BaseStaticPrint()
{
Debug.Log("BaseStaticPrint() Called.");
}
}

[LuaCallCSharp]
public class DeriveClass :BaseClass
{
public string DeriveField = "DeriveField";
public string _DeriveProperty = "BaseDeriveProperty";
public string DeriveProperty
{
set { _DeriveProperty = value; }
get { return _DeriveProperty; }
}

public static string DeriveStatic = "DeriveStatic";

public static string _DeriveStaticProperty = "DeriveStaticProperty";
public static string DeriveStaticProperty
{
set { _DeriveStaticProperty = value; }
get { return _DeriveStaticProperty; }
}

public void DerivePrint()
{
Debug.Log("DerivePrint() Called.");
}

public static void DeriveStaticPrint()
{
Debug.Log("DeriveStaticPrint() Called.");
}
}

lua脚本

--访问class
local BaseClass=CS.BaseClass--获取一个类的映射
local baseClass=CS.BaseClass()--实例化一个类
print(baseClass.BaseField); --字段
baseClass:BasePrint(); --方法
print(BaseClass.BaseStaticProperty);--静态属性
print(BaseClass.BaseStatic); --静态字段

local DeriveClass=CS.DeriveClass--获取一个子类的映射
local deriveClass=CS.DeriveClass()--实例化一个子类
deriveClass:BasePrint(); //通过子类调用父类的方法

07.XLua Lua访问C#中的方法_lua_03


04.访问复杂Class

对于参数,Lua会从左到右取C#函数中的普通参数或者ref参数依次作为自己的参数

对于返回值,Lua会从左到右取C#函数中的返回值、ref参数或者out参数依次作为自己的返回值

[LuaCallCSharp]
public class ComplexClass
{
public string ComplexFunction(int arg0, ref int arg1, string arg2, out string arg3, Parameter param)
{
Debug.Log("=========C#=========");
Debug.Log("arg0:" + arg0);
Debug.Log("arg1:" + arg1);
Debug.Log("arg2:" + arg2);
Debug.Log("arg3:");
Debug.Log("Parameter:" + param);
arg1++;
arg3 = "3(string)";
return "ComplexFunction return.";
}
}


[LuaCallCSharp]
public class Parameter
{
public int param1;
public string param2;

public override string ToString()
{
return string.Format("param1:{0} param2:{1}", param1, param2);
}
}
--访问复杂class
local ComplexClass=CS.ComplexClass--获取一个类的映射
local complexClass=CS.ComplexClass()--实例化一个类

local Parameter=CS.Parameter--获取一个类的映射
local parameter=CS.Parameter()--实例化一个类


local ret, arg1, arg3 = complexClass:ComplexFunction(
0,
1,
"2(string)",
{param1 = 3, param2 = "4(string)"}
)

print("=========Lua========")
print("ret:", ret)
print("arg1:", arg1)
print("arg3:", arg3)

07.XLua Lua访问C#中的方法_lua_04