第一部分:Xlua调用C#

1、xlua获取C#的类并调用类的构造方法

--Lua获取C#类
local GameObjectClass = CS.UnityEngine.GameObject

--使用C#类New新对象
local newGameObj = GameObjectClass('helloworld')
print(GameObjectClass, newGameObj)

 2、xlua执行C#类的静态方法

静态方法无需获取类对象,获取到类直接执行

例1:

local FindObj = CS.UnityEngineGameObject.Find('helloworld')

local deltaTime = CS.UnityEngine.Time.deltaTime

--修改C#的静态属性
CS.UnityEngine.Time.timeScale = 0.5

例2

//C#代码
public class LuaEventUtility
{
    public static void Init(int value)
    {
        Debug.LogError("value:" + value);
    }
}
--调用C#带参静态方法
CS.LuaEventUtility.ParamFunction(1)

3、获取C#类对象,并调用类对象非静态方法

调用非静态方法一定要获取到具体的C#类对象!!!

例1:获取单例对象并调用非静态方法,Singleton是单例的一种写法,网上源码很多

//Singleton是单例,网上源码很多,获取一个Instance对象
public class LuaEventUtility:Singleton<LuaEventUtility>
{
    public void ParamFunction(int a)
        Debug.Log("ParamFunction a:" + a);
}
--调用C#带参静态方法
CS.LuaEventUtility.Instance.ParamFunction(1)

4、Lua向C#注册委托回调方法


public class LuaEventUtility:Singleton<LuaEventUtility>
{
    [CSharpCallLua]
    public delegate void NoTableDelegate(int param1);

    [CSharpCallLua]
    public delegate void TableDelegate(LuaTable tab,int param1);

    public static void DoTest1(LuaTable tab, string functionName)
    {
        NoTableDelegate callback = tab.Get<NoTableDelegate>(functionName);
        callback(1111);
    }
    public static void DoTest2(LuaTable tab, string functionName)
    {
        TableDelegate callback = tab.Get<TableDelegate>(functionName);
        callback(tab,2222);
    }
}

下面是Lua调用C#的代码,我这是模拟Xlua的工程,以类的方式实现交互

--创建--
function UI_Start:OnCreate(UIElement)
    CS.LuaEventUtility.DoTest1(self,"Test1Callback")

    self.Test2Example = "AAAA"
    CS.LuaEventUtility.DoTest2(self,"Test2Callback")
end

function UI_Start:Test1Callback(param)
    print("Test1, param: ".. tostring(param))
end

function UI_Start:Test2Callback(param)
    print("Test2, param: " .. param)
    print("Test2, example: " .. self.Test2Example)
end

看Log日志发现:

Test1回调方法执行,但参数没有返回,

Test2参数打印正常,且table也传回来了,可以调用self

为何Test1的回调参数失败,由于上面的lua代码是传入的类内方法,换成table继续测试Test1,执行这个lua代码

        TestTable = {}
        TestTable.Test2Example = 'AAAA'

        function LuaStartUp()
            CS.LuaEventUtility.DoTest1(TestTable,'Test1Callback')
            CS.LuaEventUtility.DoTest2(TestTable,'Test2Callback')
        end
        function TestTable.Test1Callback(param)
            print('Test1, param: '.. tostring(param))
        end

        function TestTable.Test2Callback(tab,param)
            print('Test2, param: ' .. param)
            print('Test2, example: ' .. tab.Test2Example)
        end

        LuaStartUp()

Test打印成功,参数也没有问题。

lua调用C#Action委托使用总结:

要点1:lua代码是不允许直接调用C#的delegate变量的,我看官方教程也是先把lua的table和luafunction通过一个方法传入到C#中,方法内完成lua回调方法和C#委托的绑定。

要点2:Lua调用C#方法的参数的类型必须提前加入到Lua环境中,不添加会报错

ExampleConfig文件是官网demo给的添加参数例子,这里面把我们委托的参数都加进去,重新生成Lua

要点3:打了标记记得运行一下Xlua的Generate Code

5、Lua向C#注册Action委托

public class LuaEventUtility:Singleton<LuaEventUtility>
{    
    [LuaCallCSharp]
    public event Action<int> ActionTest3;

    [LuaCallCSharp]
    public event Action<LuaTable,int, int> ActionTest4;

    public void DoTest3()
    {
        ActionTest3.Invoke(3333);
    }

    public void DoTest4(LuaTable tab)
    {
        ActionTest4.Invoke(tab, 4444, 5555);
    }
}

 下面是Lua调用C#的代码,我这是模拟Xlua的项目工程,以类的方式实现交互

--创建--
function UI_Start:OnCreate(UIElement)
    CS.LuaEventUtility.Instance:ActionTest3('+', self.Test3Callback)
    CS.LuaEventUtility.Instance:DoTest3()

    self.Test4Example = "BBBB"
    CS.LuaEventUtility.Instance:ActionTest4('+', self.Test4Callback)
    CS.LuaEventUtility.Instance:DoTest4(self)
end

function UI_Start:Test3Callback(param)
    print("Test3, param: " .. tostring(param))
end

function UI_Start:Test4Callback(param1,param2)
    print("Test4, param: " .. param1 .. "  ,param2: " .. param2)
    print("Test4, example: " .. self.Test4Example)
end

看Log日志发现:

Test3回调方法执行,但参数没有返回,

Test4参数打印正常,且table也传回来了,可以调用self

Test3和调用委托的Test1出现了同样的问题,

换成table继续测试,执行这个lua代码

    TestTable = {}
    TestTable.Test4Example = 'BBBB'
    function TestTable.Test3Callback(param)
        print('Test3, param: ' .. tostring(param))
    end

    function TestTable.Test4Callback(tab,param1, param2)
        print('Test4, param: ' .. tostring(param1).. '  ,param2: ' .. tostring(param2))
        print('Test4, example: ' .. tab.Test4Example)
    end

    function LuaStartUp()
        CS.LuaEventUtility.Instance:ActionTest3('+', TestTable.Test3Callback)
        CS.LuaEventUtility.Instance:DoTest3()
        CS.LuaEventUtility.Instance:ActionTest4('+', TestTable.Test4Callback)
        CS.LuaEventUtility.Instance:DoTest4(TestTable)
    end

    LuaStartUp()

打印成功 

lua调用C#Action委托总结:

要点1: lua代码调用 C#的 Action要用冒号,把C#Action当成是一个方法。

要点2:C#代码的Action要用  [CSharpCallLua]标记

要点3:打了标记记得运行一下Xlua的Generate Code

下面是C#和Lua的代码

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

public class TestLuaCallback : MonoBehaviour
{
    private LuaEnv _luaEnv;

    string str1 = @"
        TestTable = {}
        TestTable.Test2Example = 'AAAA'

        function LuaStartUp()
            CS.LuaEventUtility.DoTest1(TestTable,'Test1Callback')
            CS.LuaEventUtility.DoTest2(TestTable,'Test2Callback')
        end
        function TestTable.Test1Callback(param)
            print('Test1, param: '.. tostring(param))
        end

        function TestTable.Test2Callback(tab,param)
            print('Test2, param: ' .. param)
            print('Test2, example: ' .. tab.Test2Example)
        end

        LuaStartUp()
    ";


    string str2 = @"
    TestTable = {}
    TestTable.Test4Example = 'BBBB'
    function TestTable.Test3Callback(param)
        print('Test3, param: ' .. tostring(param))
    end

    function TestTable.Test4Callback(tab,param1, param2)
        print('Test4, param: ' .. tostring(param1).. '  ,param2: ' .. tostring(param2))
        print('Test4, example: ' .. tab.Test4Example)
    end

    function LuaStartUp()
        CS.LuaEventUtility.Instance:ActionTest3('+', TestTable.Test3Callback)
        CS.LuaEventUtility.Instance:DoTest3()
        CS.LuaEventUtility.Instance:ActionTest4('+', TestTable.Test4Callback)
        CS.LuaEventUtility.Instance:DoTest4(TestTable)
    end

    LuaStartUp()
";

    // Start is called before the first frame update
    void Start()
    {
        _luaEnv = new LuaEnv();
        //_luaEnv.DoString(str1);
        _luaEnv.DoString(str2);
        //LuaFunction luaRoot = _luaEnv.Global.Get<LuaFunction>("LuaStartUp");
        //luaRoot.Call();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

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

[CSharpCallLua]
public class LuaEventUtility:Singleton<LuaEventUtility>
{
    [CSharpCallLua]
    public delegate void NoTableDelegate(int param1);
    [CSharpCallLua]
    public delegate void TableDelegate(LuaTable tab, int param1);

    public event Action<int> ActionTest3;
    [CSharpCallLua]
    public event Action<LuaTable,int, int> ActionTest4;

    public static void DoTest1(LuaTable tab, string functionName)
    {
        NoTableDelegate callback = tab.Get<NoTableDelegate>(functionName);
        callback(1111);
    }
    public static void DoTest2(LuaTable tab, string functionName)
    {
        TableDelegate callback = tab.Get<TableDelegate>(functionName);
        callback(tab,2222);
    }
    public void DoTest3()
    {
        ActionTest3.Invoke(3333);
    }

    public void DoTest4(LuaTable tab)
    {
        ActionTest4.Invoke(tab, 4444, 5555);
    }

    public void Update()
    { 
         //ActionTest3.Invoke(3333);
    }
}

 

 

更多推荐