fwalker 发表于 2021-11-19 06:37

Xlua基础(三) Lua调用C#

1.new C#对象
local newGameObj = CS.UnityEngine.GameObject()
local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
print(newGameObj, newGameObj2)2.访问静态属性,方法
local GameObject = CS.UnityEngine.GameObject
print('UnityEngine.Time.deltaTime:', CS.UnityEngine.Time.deltaTime) --读静态属性
CS.UnityEngine.Time.timeScale = 0.5 --写静态属性
print('helloworld', GameObject.Find('helloworld')) --静态方法调用3.访问成员属性,方法
local DerivedClass = CS.Tutorial.DerivedClass
local testobj = DerivedClass()--调用构造函数
testobj.DMF = 1024--设置成员属性
print(testobj.DMF)--读取成员属性
testobj:DMFunc()--成员方法4.重载方法的调用
testobj:TestFunc(100)
testobj:TestFunc('hello') lua语言没办法区别单精度双精度,所以假设拥有单双精度的重载是按顺序来调用,会调用最上面的那个重载方法。
5.可变参数方法调用
        public void VariableParamsFunc(int a, params string[] strs)
        {
                UnityEngine.Debug.Log("VariableParamsFunc: a =" + a);
                foreach (var str in strs)
                {
                        UnityEngine.Debug.Log("str:" + str);
                }
        }

      ---lua
      testobj:VariableParamsFunc(5, 'hello', 'john')6.带有结构体的参数的方法   
               在lua端定义一个表去映射结构体:
public struct MyStruct
{
   public string x;
   public string y;
}

public void CSharpFunc(MyStruct p)
{
   Debug.Log(p.x);
   Debug.Log(p.y);
}

--lua
myStructTable={x="C#",y="lua"}
obj:CSharpFunc(myStructTable)         如果是带有接口参数,则和上面相似,不过要在接口的定义上加上来为接口生成实例代码。具体使用是还是是看由哪方调用的。在这里,如果方法写在C#端,那么肯定由C#端去执行这个接口的业务,也就是说是由C#来执行lua的table的业务,那么实际上在方法的调用上是lua调用C# ,而方法内对接口内方法的调用就是CSharpCallLua.
         委托也是和接口一样,打上并且生成代码,然后由lua端写好function来传入。
7.带有多返回数值的C#方法
            C#不是只有一个返回值吗?这里还得算上带有ref out的参数。 看个官方例子:
        public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
                {
                        Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = " + p2);
                        luafunc();
                        p2 = p2 * p1.x;
                        p3 = "hello " + p1.y;
                        csfunc = () =>
                        {
                                Debug.Log("csharp callback invoked!");
                        };
                        return 1.23;
                }

--lua

   local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
               print('i am lua callback')
            end)
            print('ComplexFunc ret:', ret, p2, p3, csfunc)
            csfunc()            看起来很复杂,实际上很简单,lua中调用这个方法,传值的时候只传了3个而实际上参数有5个,原因是带out的参数不需要传值。
8.lua中不支持C#的泛型方法,但可以使用扩展方法来调用
          直接看官方例子:
public void GenericMethod<T>()
{
        Debug.Log("GenericMethod<" + typeof(T) + ">");
}


public static class DerivedClassExtensions
{
        public static int GetSomeData(this DerivedClass obj)
        {
                Debug.Log("GetSomeData ret = " + obj.DMF);
                return obj.DMF;
        }

        public static int GetSomeBaseData(this BaseClass obj)
        {
                Debug.Log("GetSomeBaseData ret = " + obj.BMF);
                return obj.BMF;
        }

        public static void GenericMethodOfString(this DerivedClass obj)
        {
                obj.GenericMethod<string>();
        }
}         


print(testobj:GetSomeData())
print(testobj:GetSomeBaseData()) --访问基类的Extension methods
testobj:GenericMethodOfString()--通过Extension methods实现访问泛化方法9.事件
public event Action TestEvent;


--lua

local function lua_event_callback1() print('lua_event_callback1') end
local function lua_event_callback2() print('lua_event_callback2') end
testobj:TestEvent('+', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('+', lua_event_callback2)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback2)10.委托
               public Action<string> TestDelegate = (param) =>
               {
                          Debug.Log("TestDelegate in c#:" + param);
               };


--lua

            testobj.TestDelegate('hello') --直接调用
            local function lua_delegate(str)
                print('TestDelegate in lua:', str)
            end
            testobj.TestDelegate = lua_delegate + testobj.TestDelegate --combine,这里演示的是C#delegate作为右值,左值也支持
            testobj.TestDelegate('hello')
            testobj.TestDelegate = testobj.TestDelegate - lua_delegate --remove
            testobj.TestDelegate('hello')

jquave 发表于 2021-11-19 06:43

你这里是lua访问lua里面创建的c#对象,但是lua怎么访问c#里面创建的c#对象?

xiangtingsl 发表于 2021-11-19 06:45

找到C#端的引用,带上CS前缀访问不就可以了吗

franciscochonge 发表于 2021-11-19 06:45

意思是只能通过CS静态方法去获取实例对象?

ainatipen 发表于 2021-11-19 06:46

C#端的引用是什么?所有的例子都是获取C#类,不能直接访问某个C#对象

kirin77 发表于 2021-11-19 06:51

你在C#端创建对象,总有一个类中保存了它的引用吧。你去在lua中访问这个类中的这个引用就好了。你可以看这个系列的第二篇,是lua访问c#的
页: [1]
查看完整版本: Xlua基础(三) Lua调用C#