toluaframework中C#怎么调用Lua的方法以及无GC方法

toluaframework中C#怎么调用Lua的方法

问题

用过luaframework框架的人应该都知道框架提供了Util的工具类,工具类提供了一个方法就是Util.CallMethod的方法,方便Unity中直接调用lua层的方法。

Util.CallMethod

csharp 复制代码
        /// <summary>
        /// 执行Lua方法
        /// </summary>
        public static object[] CallMethod(string module, string func, params object[] args)
        {
            LuaManager luaMgr = AppFacade.Instance.GetManager<LuaManager>(ManagerName.Lua);
            if (luaMgr == null)
            {
                Debug.LogError("lua模块"" + module + ""未被找到");
                return null;
            }
            return luaMgr.CallFunction(module + "." + func, args);
        }

LuaManager.CallFunction

下面这里使用的框架提供的方法,但是这个方法是有GC的,我们在进入到luaMgr.CallFunction中查看一下

csharp 复制代码
// Update is called once per frame
        public object[] CallFunction(string funcName, params object[] args) {
            LuaFunction func = lua.GetFunction(funcName);
            if (func != null) {
                return func.LazyCall(args);
            }
            return null;
        }

从上面的代码中我们可以看到实际上他调用的是LazyCall(),这个方法的返回值是object[],这必然是需要我们自己拆箱装箱,有GC的操作,尽可能避免拆箱装箱可以减少性能的消耗。

LuaFunction.LazyCall

这个方法作者也提示了是有GC的,这里让我们使用的是Invoke的方法调用,这样我们不需要有装箱拆箱的转换操作,这里可以直接转换成我们需要的数据。

csharp 复制代码
        //慎用, 有gc alloc
        [System.Obsolete("LuaFunction.LazyCall() is obsolete.Use LuaFunction.Invoke()")]
        public object[] LazyCall(params object[] args)
        {
            BeginPCall();
            int count = args == null ? 0 : args.Length;

            if (!luaState.LuaCheckStack(count + 6))
            {
                EndPCall();
                throw new LuaException("stack overflow");
            }
            
            PushArgs(args);
            PCall();
            object[] objs = luaState.CheckObjects(oldTop);
            EndPCall();
            return objs;
        }

解决方案

这里我只举一个例子写一个两个参数的方法,多个参数的方法模仿我这个自己写就好了,需要多少参数写多少个方法即可

LuaFunction脚本

我们先看看这个脚本作者给我们提供了什么

这里作者给我们提供了无返回值的多参数Call 的方法,也提供了有返回值的多参数Invoke

无GC消耗的调用

找到LuaManager脚本中原始有GC消耗的脚本,在他下方我们加两个多态写法

添加两个无GC消耗的多态方法

csharp 复制代码
 	// Update is called once per frame 原始有GC的方法
    public object[] CallFunction(string funcName, params object[] args) {
        LuaFunction func = lua.GetFunction(funcName);
        if (func != null) {
            return func.LazyCall(args);//这里有GC
        }
        return null;
    }

	//有返回值并且有两个参数的无GC调用方法
    public R CallFunction<T1,T2,R>(string funcName, T1 t1,T2 t2)
    {
        LuaFunction func = lua.GetFunction(funcName);
        if (func != null)
        {
            return func.Invoke<T1,T2,R>(t1,t2);
        }
        return default;
    }

	//无返回值并且有两个参数的无GC调用方法
    public void CallFunction<T1, T2>(string funcName, T1 t1, T2 t2)
    {
        LuaFunction func = lua.GetFunction(funcName);
        if (func != null)
        {
            func.Call<T1, T2>(t1, t2);
        }
    }

打开Util工具类脚本,找到原始有GC的工具类调用方法

在这下方我们写两个调用方法封装一下

csharp 复制代码
		/// <summary>
        /// 执行Lua方法 有GC
        /// </summary>
        public static object[] CallMethod(string module, string func, params object[] args)
        {
            LuaManager luaMgr = AppFacade.Instance.GetManager<LuaManager>(ManagerName.Lua);
            if (luaMgr == null)
            {
                Debug.LogError("lua模块"" + module + ""未被找到");
                return null;
            }
            return luaMgr.CallFunction(module + "." + func, args);
        }

		//无返回值无GC两个参数的方法
        public static void NoGCCallMethod<T1, T2>(string module, string func, T1 t1, T2 t2)
        {
            LuaManager luaMgr = AppFacade.Instance.GetManager<LuaManager>(ManagerName.Lua);
            if (luaMgr == null)
            {
                Debug.LogError("lua模块"" + module + ""未被找到");
            }
            luaMgr.CallFunction(module + "." + func, t1 ,t2);
        }

		//有返回值无GC两个参数的方法
        public static R InvokeMethod<T1, T2, R>(string module, string func, T1 t1, T2 t2)
        {
            LuaManager luaMgr = AppFacade.Instance.GetManager<LuaManager>(ManagerName.Lua);
            if (luaMgr == null)
            {
                Debug.LogError("lua模块"" + module + ""未被找到");
                return default;
            }
            return luaMgr.CallFunction<T1, T2, R>(module + "." + func, t1, t2);
        }

用法总结

下面是原始有GC的调用方法和无GC的调用方法,调用参考

相关推荐
難釋懷4 小时前
Lua语法入门-条件控制、函数
开发语言·junit·lua
第二层皮-合肥9 小时前
基于C#的工业测试控制软件-总体框架
开发语言·c#
steins_甲乙11 小时前
C# 通过共享内存与 C++ 宿主协同捕获软件窗口
开发语言·c++·c#·内存共享
似水明俊德14 小时前
12-C#.Net-加密解密-学习笔记
笔记·学习·oracle·c#·.net
阿蒙Amon14 小时前
C#常用类库-详解SSH.NET
c#·ssh·.net
似水明俊德14 小时前
11-C#.Net-多线程-Async-Await篇-学习笔记
开发语言·笔记·学习·c#·.net
美团骑手阿豪15 小时前
C#语法:HashSet与List对比,适合场景
unity·c#
wr15 小时前
Modbus 读写转换
c#
公子小六15 小时前
基于.NET的Windows窗体编程之WinForms入门简介
windows·microsoft·c#·.net
CSharp精选营15 小时前
C# 面试高频题:装箱和拆箱是如何影响性能的?
c#·.net·面试题·装箱·拆箱·c#面试题