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的调用方法,调用参考

相关推荐
dotent·3 小时前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
合作小小程序员小小店3 小时前
桌面开发,超市管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·sqlserver·c#
软件测试雪儿4 小时前
高频Postman软件测试面试题
测试工具·lua·postman
合作小小程序员小小店5 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
p***32356 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
2501_941807268 小时前
Java高性能消息队列与Kafka实战分享:大规模消息处理、异步通信与性能优化经验
c#·linq
周杰伦fans9 小时前
C# 中的**享元工厂**模式
开发语言·数据库·c#
鹿衔`9 小时前
通过Flink 1.19 客户端实现Flink集群连接 Kafka 基础测试报告
c#·linq
玩泥巴的11 小时前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书