lua代码
Assets/Lua/Test1.lua.txt
function IntParam(p) print("~~~~~ IntParam") print(type(p), p) print("~~~~~") return p end function Vec3Param(p) print("~~~~~ Vec3Param") print(type(p), p.x, p.y, p.z) print("~~~~~") return p end function EnumParam(p) print("~~~~~ EnumParam") print(type(p), p) print("~~~~~") return p end function ArrayParam(p) print("~~~~~ ArrayParam") print(type(p), p.Length) for i=0,p.Length-1 do print(i, p[i]) end print("~~~~~") return p end function RefOutParam(p1, p2) print("~~~~~ RefOutParam") print(type(p1), p1, type(p2), p2) return 1, 2, 3 end function MyStructParam(p) print("~~~~~ ArrayParam") print(type(p), p.str, p.value.i, p.value.f) print("~~~~~") return p end function ReturnTable() return { a = 1, b =2 } end
1, 将lua函数映射为c#委托的方式
委托部分
[CSharpCallLua] public delegate int IntParamDelegate(int p); [CSharpCallLua] public delegate int Vec3ParamDelegate(Vector3 p); [CSharpCallLua] public delegate int EnumParamDelegate(StringSplitOptions opts); [CSharpCallLua] public delegate Array ArrayParamDelegate(Array arr); [CSharpCallLua] public delegate int RefOutParamDelegate(ref int p1, out int p2); [GCOptimize] [LuaCallCSharp] public struct MyStruct1 { public int i; public float f; } [GCOptimize] [LuaCallCSharp] public struct MyStruct2 { public MyStruct1 value; public string str; } [CSharpCallLua] public delegate MyStruct2 MyStructParamDelegate(MyStruct2 p);
映射并调用代码
public class Test1 : MonoBehaviour { private LuaEnv m_LuaEnv; private IntParamDelegate m_IntParamDelegate; private Vec3ParamDelegate m_Vec3ParamDelegate; private EnumParamDelegate m_EnumParamDelegate; private ArrayParamDelegate m_ArrayParamDelegate; private RefOutParamDelegate m_RefOutParamDelegate; private MyStructParamDelegate m_MyStructParamDelegate; public Func<int, int> m_IntParamFunc; void Start() { m_LuaEnv = new LuaEnv(); m_LuaEnv.AddLoader((ref string filePath) => { filePath = filePath.Replace('.', '/'); filePath = $"Assets/{filePath}.lua.txt"; var txtAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(filePath); return Encoding.UTF8.GetBytes(txtAsset.text); }); m_LuaEnv.DoString("require('Lua.Test1')"); m_LuaEnv.Global.Get("IntParam", out m_IntParamDelegate); m_LuaEnv.Global.Get("IntParam", out m_IntParamFunc); m_LuaEnv.Global.Get("Vec3Param", out m_Vec3ParamDelegate); m_LuaEnv.Global.Get("EnumParam", out m_EnumParamDelegate); m_LuaEnv.Global.Get("ArrayParam", out m_ArrayParamDelegate); m_LuaEnv.Global.Get("RefOutParam", out m_RefOutParamDelegate); m_LuaEnv.Global.Get("MyStructParam", out m_MyStructParamDelegate); } void OnDestroy() { if (null != m_LuaEnv) { //必须先置为null, 否则Dispose报错 m_IntParamDelegate = null; m_IntParamFunc = null; m_Vec3ParamDelegate = null; m_EnumParamDelegate = null; m_ArrayParamDelegate = null; m_RefOutParamDelegate = null; m_MyStructParamDelegate = null; m_LuaEnv.Dispose(); } } void Update() { if (Input.GetKeyDown(KeyCode.Alpha1)) { m_IntParamDelegate(1); m_IntParamFunc(2); m_Vec3ParamDelegate(new Vector3(1, 2, 3)); m_EnumParamDelegate(StringSplitOptions.RemoveEmptyEntries); m_ArrayParamDelegate(new int[] { 1, 2, 3 }); int i1 = 0; m_RefOutParamDelegate(ref i1, out var i2); //lua中的第1个是本身返回值, 第2个为ref参数, 第3个为out参数 Debug.Log($"i1:{i1}, i2:{i2}"); var myStruct = new MyStruct2(); myStruct.str = "hello"; myStruct.value.i = 10; myStruct.value.f = 2.3f; m_MyStructParamDelegate(myStruct); } } }
2, LuaFunction类方式
public class Test1 : MonoBehaviour { private LuaEnv m_LuaEnv; public LuaFunction m_LuaFuncIntParam; public LuaFunction m_LuaFuncVec3Param; public LuaFunction m_LuaFuncArrayParam; public LuaFunction m_LuaFuncRefOutParam; public LuaFunction m_LuaFuncMyStructParam; void Start() { m_LuaEnv = new LuaEnv(); m_LuaEnv.AddLoader((ref string filePath) => { filePath = filePath.Replace('.', '/'); filePath = $"Assets/{filePath}.lua.txt"; var txtAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(filePath); return Encoding.UTF8.GetBytes(txtAsset.text); }); m_LuaEnv.DoString("require('Lua.Test1')"); m_LuaEnv.Global.Get("IntParam", out m_LuaFuncIntParam); m_LuaEnv.Global.Get("Vec3Param", out m_LuaFuncVec3Param); m_LuaEnv.Global.Get("ArrayParam", out m_LuaFuncArrayParam); m_LuaEnv.Global.Get("RefOutParam", out m_LuaFuncRefOutParam); m_LuaEnv.Global.Get("MyStructParam", out m_LuaFuncMyStructParam); } void OnDestroy() { if (null != m_LuaEnv) { m_LuaEnv.Dispose(); } } void Update() { if (Input.GetKeyDown(KeyCode.Alpha1)) { m_LuaFuncIntParam.Func<int, int>(3); m_LuaFuncVec3Param.Func<Vector3, Vector3>(new Vector3(5, 6, 7)); m_LuaFuncArrayParam.Func<Array, Array>(new string[] { "a", "b", "c" }); var results = m_LuaFuncRefOutParam.Call(0); foreach (var item in results) { Debug.Log($"{item.GetType().Name}:{item}"); } var myStruct = new MyStruct2(); myStruct.str = "hello"; myStruct.value.i = 10; myStruct.value.f = 2.3f; m_LuaFuncMyStructParam.Func<MyStruct2, MyStruct2>(myStruct); var luaFunc = m_LuaEnv.Global.Get<LuaFunction>("ReturnTable"); results = luaFunc.Call(); foreach (var item in results) { Debug.Log($"{item.GetType().Name}:{item}"); } } } }
参考
xLua-C#访问lua中的全局function_fdelegate xlua-CSDN博客
标签:Get,c#,Global,xlua,lua,LuaEnv,print,public,out From: https://www.cnblogs.com/sailJs/p/18037210