7.C#调用Lua函数

7.CSharp调用Lua-函数的获取和调用


7.1 知识点

准备工作

初始化Lua管理器,创建Lua脚本并在C#脚本调用

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson07_CSharp调用Lua_函数的获取和调用");

给Lua脚本添加不同类型的函数

print('我是Lua脚本 Lesson07_CSharp调用Lua_函数的获取和调用')

-- 无参无返回值
testFun1 = function()
    print("我是Lua无参无返回值函数")
end

-- 有参有返回值
testFun2 = function(a)
    print("我是Lua有参有返回值函数")
    return a + 1
end

-- 多返回值函数
testFun3 = function(a)
    print("我是Lua多返回值函数")
    return 1, 2, false, "123", a
end

-- 变长参数函数
testFun4 = function(a, ...)
    print("我是Lua变长参数函数")
    print(a)
    arg = {...}
    for k,v in pairs(arg) do
        print(k,v)
    end
end

无参无返回值函数的获取和调用

// 自定义无参无返回值的委托
public delegate void TestFun1();

// 自定义委托
TestFun1 testFun1 = LuaManager.Instance.Global.Get<TestFun1>("testFun1");
testFun1();//我是Lua无参无返回值函数

// C#委托
Action testFun1Action = LuaManager.Instance.Global.Get<Action>("testFun1");
testFun1Action();//我是Lua无参无返回值函数

// Unity委托
UnityAction testFun1UnityAction = LuaManager.Instance.Global.Get<UnityAction>("testFun1");
testFun1UnityAction();//我是Lua无参无返回值函数

// Xlua获取函数 尽量少用 会产生一些垃圾
LuaFunction testFun1LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun1");
// LuaFunction函数通过Call方法调用
testFun1LuaFunction.Call();//我是Lua无参无返回值函数

有参有返回值函数的获取和调用

// 自定义有参有返回值的委托
// 需要添加[CSharpCallLua]特性 该特性是在XLua命名空间中的 加了过后 要在编辑器里点击生成xLua代码
[CSharpCallLua]
public delegate int TestFun2(int a);

// 自定义委托
TestFun2 testFun2 = LuaManager.Instance.Global.Get<TestFun2>("testFun2");
Debug.Log("有参有返回值:" + testFun2(10));//11

// C#委托
Func<int, int> testFun2Func = LuaManager.Instance.Global.Get<Func<int, int>>("testFun2");
Debug.Log("有参有返回值:" + testFun2Func(20));//21

// Xlua获取函数
LuaFunction testFun2LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun2");
// LuaFunction函数调用单返回值方法 要传入参数和返回值数组的第一个元素[0]
Debug.Log("有参有返回值:" + testFun2LuaFunction.Call(30)[0]);//31

多返回值函数的获取和调用

//自定义多返回值的委托 第一个a代表Lua函数的参数 第一个返回值通过调用委托获得 其他返回值通过传入out或ref参数获得 
[CSharpCallLua]
public delegate int TestFun31(int a, out int b, out bool c, out string d, out int e);
[CSharpCallLua]
public delegate int TestFun32(int a, ref int b, ref bool c, ref string d, ref int e);

// 使用out来接收多返回值
TestFun31 testFun31 = LuaManager.Instance.Global.Get<TestFun31>("testFun3");
int b1;
bool c1;
string d1;
int e1;
Debug.Log("第一个返回值:" + testFun31(100, out b1, out c1, out d1, out e1) + " 其他返回值:" + b1 + "_" + c1 + "_" + d1 + "_" + e1);//第一个返回值:1 其他返回值:2_False_123_100

// 使用ref来接收多返回值 
TestFun32 testFun32 = LuaManager.Instance.Global.Get<TestFun32>("testFun3");
// 使用ref的变量必须初始化
int b2 = 0;
bool c2 = true;
string d2 = "";
int e2 = 0;
Debug.Log("第一个返回值:" + testFun32(200, ref b2, ref c2, ref d2, ref e2) + "其他返回值:" + b2 + "_" + c2 + "_" + d2 + "_" + e2);//第一个返回值:1 其他返回值:2_False_123_100

// Xlua获取函数
LuaFunction testFun3LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun3");
// LuaFunction函数通过Call方法调用多返回值会返回返回值数组
object[] objs = testFun3LuaFunction.Call(1000);
for (int i = 0; i < objs.Length; ++i)
{
    Debug.Log("第" + i + "个返回值是:" + objs[i]);
}

变长参数函数的获取和调用

//变长参数的委托
[CSharpCallLua]
//变长参数的类型 是根据实际情况来定的
//比如 如果知道Lua的函数只会传int的话也可以使用int的类型的变长参数数组
public delegate void TestFun4(string a, params object[] args);

//自定义委托
TestFun4 testFun4 = LuaManager.Instance.Global.Get<TestFun4>("testFun4");
testFun4("123", 1, 2, 3, 4, 5, 566, 7, 7, 8, 9, 99);
//这样可能成功遍历,也可能遍历不出来
//可能会出现:LUA: 1	System.Object[]: -1008033792
//未稳定遍历时,与变长参数在 Lua 侧的表结构有关;可改用 LuaFunction.Call 调用,或减少委托侧 params 与 Lua `...` 的混用

//Xlua获取函数
LuaFunction testFun4LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun4");
testFun4LuaFunction.Call("456", 6, 7, 8, 99, 1);//成功遍历

7.2 知识点代码

Lesson07_CSharp调用Lua_函数的获取和调用.lua

print('我是Lua脚本 Lesson07_CSharp调用Lua_函数的获取和调用')

--无参无返回值
testFun1 = function()
    print("我是Lua无参无返回值函数")
end

--有参有返回值
testFun2 = function(a)
    print("我是Lua有参有返回值函数")
    return a + 1
end

--多返回值函数
testFun3 = function(a)
    print("我是Lua多返回值函数")
    return 1, 2, false, "123", a
end

--变长参数函数
testFun4 = function(a, ...)
    print("我是Lua变长参数函数")
    print(a)
    arg = {...}
    for k,v in pairs(arg) do
        print(k,v)
    end
end

Lesson07_CSharp调用Lua_函数的获取和调用

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

//自定义无参无返回值的委托
public delegate void TestFun1();

//自定义有参有返回值的委托
//需要添加[CSharpCallLua]特性 该特性是在XLua命名空间中的 加了过后 要在编辑器里点击生成xLua代码
[CSharpCallLua]
public delegate int TestFun2(int a);

//自定义多返回值的委托 第一个a代表Lua函数的参数 第一个返回值通过调用委托获得 其他返回值通过传入out或ref参数获得 
[CSharpCallLua]
public delegate int TestFun31(int a, out int b, out bool c, out string d, out int e);
[CSharpCallLua]
public delegate int TestFun32(int a, ref int b, ref bool c, ref string d, ref int e);

//变长参数的委托
[CSharpCallLua]
//变长参数的类型 是根据实际情况来定的
//比如 如果知道Lua的函数只会传int的话也可以使用int的类型的变长参数数组
public delegate void TestFun4(string a, params object[] args);


public class Lesson07_CSharp调用Lua_函数的获取和调用 : MonoBehaviour
{
    void Start()
    {
        LuaManager.Instance.Init();

        LuaManager.Instance.DoLuaFile("Lesson07_CSharp调用Lua_函数的获取和调用");

        #region 知识点一 无参无返回值函数的获取和调用

        //自定义委托
        TestFun1 testFun1 = LuaManager.Instance.Global.Get<TestFun1>("testFun1");
        testFun1();//我是Lua无参无返回值函数

        //C#委托
        Action testFun1Action = LuaManager.Instance.Global.Get<Action>("testFun1");
        testFun1Action();//我是Lua无参无返回值函数

        //Unity委托
        UnityAction testFun1UnityAction = LuaManager.Instance.Global.Get<UnityAction>("testFun1");
        testFun1UnityAction();//我是Lua无参无返回值函数

        //Xlua获取函数 尽量少用 会产生一些垃圾
        LuaFunction testFun1LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun1");
        //LuaFunction函数通过Call方法调用
        testFun1LuaFunction.Call();//我是Lua无参无返回值函数

        #endregion

        #region 知识点二 有参有返回值函数的获取和调用

        //自定义委托
        TestFun2 testFun2 = LuaManager.Instance.Global.Get<TestFun2>("testFun2");
        Debug.Log("有参有返回值:" + testFun2(10));//11

        //C#委托
        Func<int, int> testFun2Func = LuaManager.Instance.Global.Get<Func<int, int>>("testFun2");
        Debug.Log("有参有返回值:" + testFun2Func(20));//21

        //Xlua获取函数
        LuaFunction testFun2LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun2");
        //LuaFunction函数调用单返回值方法 要传入参数和返回值数组的第一个元素[0]
        Debug.Log("有参有返回值:" + testFun2LuaFunction.Call(30)[0]);//31

        #endregion

        #region 知识点三 多返回值函数的获取和调用

        //使用out来接收多返回值
        TestFun31 testFun31 = LuaManager.Instance.Global.Get<TestFun31>("testFun3");
        int b1;
        bool c1;
        string d1;
        int e1;
        Debug.Log("第一个返回值:" + testFun31(100, out b1, out c1, out d1, out e1) + " 其他返回值:" + b1 + "_" + c1 + "_" + d1 + "_" + e1);//第一个返回值:1 其他返回值:2_False_123_100

        //使用ref来接收多返回值 
        TestFun32 testFun32 = LuaManager.Instance.Global.Get<TestFun32>("testFun3");
        //使用ref的变量必须初始化
        int b2 = 0;
        bool c2 = true;
        string d2 = "";
        int e2 = 0;
        Debug.Log("第一个返回值:" + testFun32(200, ref b2, ref c2, ref d2, ref e2) + "其他返回值:" + b2 + "_" + c2 + "_" + d2 + "_" + e2);//第一个返回值:1 其他返回值:2_False_123_100

        //Xlua获取函数
        LuaFunction testFun3LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun3");
        //LuaFunction函数通过Call方法调用多返回值会返回返回值数组
        object[] objs = testFun3LuaFunction.Call(1000);
        for (int i = 0; i < objs.Length; ++i)
        {
            Debug.Log("第" + i + "个返回值是:" + objs[i]);
        }

        #endregion

        #region 知识点四 变长参数函数的获取和调用

        //自定义委托
        TestFun4 testFun4 = LuaManager.Instance.Global.Get<TestFun4>("testFun4");
        testFun4("123", 1, 2, 3, 4, 5, 566, 7, 7, 8, 9, 99);//可能会出现:LUA: 1	System.Object[]: -1008033792
        //这样可能成功遍历,也可能遍历不出来
        //未稳定遍历时与变长参数在 Lua 侧表结构有关;可改用 LuaFunction.Call 或减少 params 与 Lua `...` 的混用

        //Xlua获取函数
        LuaFunction testFun4LuaFunction = LuaManager.Instance.Global.Get<LuaFunction>("testFun4");
        testFun4LuaFunction.Call("456", 6, 7, 8, 99, 1);//成功遍历

        #endregion
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏