Lua2GlobalFunction
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; namespace DM08 { public class Lua2GlobalFunction : MonoBehaviour { LuaEnv xLuaEnv; #region 生命 void Start() { xLuaEnv = new LuaEnv(); xLuaEnv.DoString("require 'SourceFile08'"); // UseGlobalFunction(); } private void OnDestroy() { xLuaEnv.Dispose(); } #endregion #region 辅助1 ///
映射不了带Key的(包括函数)void UseGlobalFunction() { print("UseGlobalFunction()"); NoPara(); HavePara(); CustomDelegate(); CustomDelegate_Return(); CustomDelegate_ReturnOut(); CustomDelegate_ReturnRef(); } #endregion #region 辅助2 ///
无参数void NoPara() { Action act1 = xLuaEnv.Global.Get("func1"); act1(); // xLuaEnv.Dispose();//Dispose()放着,前面没有act1 = null;会报错 } ///
有参数void HavePara() { Action act1 = xLuaEnv.Global.Get>("func2"); act1(1, 2); } #endregion #region 辅助2 自定义委托 void CustomDelegate() { Func3 act = xLuaEnv.Global.Get("func3"); act(1, 2); } [CSharpCallLua] delegate void Func3(int a, int b); void CustomDelegate_Return() { Func4 act = xLuaEnv.Global.Get("func4"); int sum = act(1, 2); print(sum); } [CSharpCallLua] delegate int Func4(int a, int b); void CustomDelegate_ReturnOut() { Func5 act = xLuaEnv.Global.Get("func5"); int resa; int resb; int sum=act(1, 2, out resa, out resb); print(sum); print(resa); print(resb); } [CSharpCallLua] delegate int Func5(int a, int b,out int resa,out int resb); void CustomDelegate_ReturnRef() { Func6 act = xLuaEnv.Global.Get("func6"); int resa=0; int resb=0; int sum = act(1, 2, ref resa, ref resb); print(sum); print(resa); print(resb); } [CSharpCallLua] delegate int Func6(int a, int b, ref int resa, ref int resb); #endregion } } -------------------------------------- function func1() print("func1") end function func2(a,b) print("func2") print(a) print(b) end function func3(a,b) print("func3") print(a) print(b) end function func4(a,b) print("func4") print(a) print(b) return a+b end function func5(a,b) print("func5") print(a) print(b) return a+b,a,b end function func6(a,b) print("func6") print(a) print(b) return a+b,a,b end,>