Lesson5_C#调用Lua_CallFunction

 //无参无返回值
 public delegate void CustomCall();
 //其他的委托都要加特性了,并且每次声明一种,都要去点击一下Xlua编辑器中的生成脚本选项
 [CSharpCallLua]
 public delegate int CustomCall2(int a);
 [CSharpCallLua]
 public delegate int CustomCall3(int a, out bool b, out string c);
 [CSharpCallLua]
 public delegate void CustomCall4(string a, params object[] args);//根据实际情况,不确定参数类型那就放object类型        

    //调用lua中的function
    LuaManager.GetInstance().Init();
    LuaManager.GetInstance().DoLuaFIle("Main"); //LuaManager见Lesson3
    
    //访问无参无返回的func(自带的委托都可以获取)
    CustomCall call = LuaManager.GetInstance().Global.Get<CustomCall>("testFun");
    call.Invoke();
    //Lua脚本中的testFun
    testFun=function (  )
      print("无参无返回")
    end

    
    //有参有返回类型
    CustomCall2 call2 = LuaManager.GetInstance().Global.Get<CustomCall2>("testFun2");
    print(call2(2));
    Func<int, int> func = LuaManager.GetInstance().Global.Get < Func<int, int>>("testFun2");
    //Lua脚本中的testFun2
    testFun2=function (a )
      print("有参有返回")
      return a+1
    end

    //Lua中的多返回值,使用out或ref来接收,第一个返回值决定委托的返回值类型,其他的返回值就用out,ref
    //举例使用out,ref也同理,重新声明一个ref的委托
    CustomCall3 call3 = LuaManager.GetInstance().Global.Get<CustomCall3>("testFun3");
    bool c; string d;
    print(call3(1, out c, out d));//这里根据Lua脚本中的函数会打印出1,false,“123”
    print(c+" "+ d);
    //Lua脚本中的testFun3
    testFun3=function (a)
      print("多返回")
      return a,false,"123"
    end

    //变长参数
    CustomCall4 call4 = LuaManager.GetInstance().Global.Get<CustomCall4>("testFun4");
    call4("123", 1, 2, 3, 4);
    //Lua脚本中的testFun4
    testFun4=function ( a,... )
      print("变长参数函数")
      arg={...}
      for k,v in pairs(arg) do
	      print(k,v)
      end
    end
posted @ 2024-02-28 22:39  波奇型蠢驴  阅读(4)  评论(0)    收藏  举报