C#调用Lua 5、从lua中获取全局函数

首先老样子:

创建Lua解析器,执行Dotring("require('lua脚本')")

之前获取全局变量使,我们都创建了相应变量的对象去存储。 那么lua中相应函数类型的变量我们怎么存储呢?

C#的委托!刚好满足了需求。

lua中的函数

//无参无返回的获取

 我们首先先定义一个无参无返回的委托:

public delegate void CustomCall();

然后和引用Lua全局变量一样,使用函数就是调用创建的委托对象。

        //无参无返回的获取
        //委托
        CustomCall call = LuaManager.GetInstance().Global.Get<CustomCall>("testFun");
        call();

以上是我们自己创建的无参无返回委托。还有三种:

        //unity自带委托
        UnityAction ua = LuaManager.GetInstance().Global.Get<UnityAction>("testFun");
        ua();

        //c#自带委托
        Action ac = LuaManager.GetInstance().Global.Get<Action>("testFun");
        ac();

        //xlua提供的一种获取函数的方式 少用
        LuaFunction lf = LuaManager.GetInstance().Global.Get<LuaFunction>("testFun");
        lf.Call();

注意:Xlua提供了一种无参无返回的方法。这个知识点很重要,将会对下面进行一些解释。

//有参有返回的获取

首先我们和无参无返回一样创建委托:

public delegate int CustomCall2(int a);
        //********************************************************************************
        //有参有返回的获取(需要加特效)
        CustomCall2 call2 = LuaManager.GetInstance().Global.Get<CustomCall2>("testFun2");
        int q =call2(1);

        //C#自带
       Func<int,int> sFun = LuaManager.GetInstance().Global.Get<Func<int, int>("testFun2");
        Debug.Log("有参有返回:" + sFun(10));

        //xlua提供的一种获取函数的方式
        LuaFunction lf2 = LuaManager.GetInstance().Global.Get<LuaFunction>("testFun2");
        Debug.Log("有参有返回:" + lf2.Call(30)[0]);

然后就报错了

 它说,这个类型必须添加CSharpCallLua。

为什么无参无返回没有这个报错,有参有返回报错了呢?因为无参无返回xlua以及提供了对应语法。

想要获取有参有返回的lua函数需要在C#的委托上加上[CSharpCallLua]特性

[CSharpCallLua]
public delegate int CustomCall2(int a);

//lua中多返回值的函数

C#中不存在多返回值函数,因此我们需要另辟蹊径。使用out 和 ref来完成lua多返回值

定义委托:

[CSharpCallLua]//多返回值
public delegate int  CustomCall3(int a,out int b,out bool c,out string d,out int e);

 out值 不需要初始化

//********************************************************************************
        //多返回值
        //使用out 和 ref来使用
        CustomCall3 call3 = LuaManager.GetInstance().Global.Get<CustomCall3>("testFun3");
        int b;
        bool c;
        string d;
        int e;
        Debug.Log("第一个返回值:" + call3(100, out b, out c, out d, out e));
        Debug.Log(b+"-"+c+"_"+d+"_"+e);

此委托的返回值 为第一个值int类型 1,之后的返回值都存储在out对象中。

 ref值需要初始化:

        [CSharpCallLua]
        public delegate int CustomCall4(int a, ref int b, ref bool c, ref string d, ref int e);

        CustomCall4 call4 = LuaManager.GetInstance().Global.Get<CustomCall4>("testFun3");
        int b1= 0;
        bool c1 = true;
        string d1 = "";
        int e1 = 0;
        Debug.Log("第一个返回值:" + call4(200,ref b1,ref c1,ref d1,ref e1));
        Debug.Log(b + "-" + c + "_" + d + "_" + e);

xlua

        //Xlua
        LuaFunction lf3 = LuaManager.GetInstance().Global.Get<LuaFunction>("testFun3");
        //Debug.Log("有参有返回:" + lf3.Call(30)[0]);
        object[] obj =  lf3.Call(1000);//相当于执行lua中的testFun3(1000)返回的是一个数组
        for (int i = 0; i < obj.Length; i++)
        {
            Debug.Log("第" + i + "个输出为" + obj[i]);
        }

 

 C#调用lua变长参数

[CSharpCallLua]
//如果有多个类型的参数,则params修饰的参数数组要放到最后,并且只使用一次
public delegate int CustomCall5(string a,params object[] args );

//如果不知道定义函数内部的类型那就用Object,如果知道函数的多返回值类型,例如都为int的话定义为int[ ] i

//变长参数
CustomCall5 call5 = LuaManager.GetInstance().Global.Get<CustomCall5>("testFun4");
call5("123", 1, 2, 3, 4, 5, "string");

LuaFunction lf4 = LuaManager.GetInstance().Global.Get<LuaFunction>("testFun4");
lf4.Call("456", 4, 5, 6, 4, 96);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值