ToLua 入门04_CallLuaFunction

前面几章说到了怎么执行lua文件,本章就应该到如何调用lua类文件中的方法;具体demo详见lua框架中的案例场景ToLua/Examples/03_CallLuaFunction,运行后可以看到例子中的打印。
我们先来看看并分析一下案例类:

using UnityEngine;
using System.Collections;
using LuaInterface;
using System;

public class CallLuaFunction : MonoBehaviour 
{
    private string script =
        @"  function luaFunc(num)                        
                return num + 1
            end

            test = {}
            test.luaFunc = luaFunc
        ";

    LuaFunction luaFunc = null;
    LuaState lua = null;
    string tips = null;
	
	void Start () 
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        lua = new LuaState();
        lua.Start();
        DelegateFactory.Init();        
        lua.DoString(script);

        //Get the function object
        luaFunc = lua.GetFunction("test.luaFunc");

        if (luaFunc != null)
        {
            int num = luaFunc.Invoke<int, int>(123456);
            Debugger.Log("generic call return: {0}", num);

            num = CallFunc();
            Debugger.Log("expansion call return: {0}", num);

            Func<int, int> Func = luaFunc.ToDelegate<Func<int, int>>();
            num = Func(123456);
            Debugger.Log("Delegate call return: {0}", num);
            
            num = lua.Invoke<int, int>("test.luaFunc", 123456, true);
            Debugger.Log("luastate call return: {0}", num);
        }

        lua.CheckTop();
	}

    void ShowTips(string msg, string stackTrace, LogType type)
    {
        tips += msg;
        tips += "\r\n";
    }

#if !TEST_GC
    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 150, 400, 300), tips);
    }
#endif

    void OnDestroy()
    {
        if (luaFunc != null)
        {
            luaFunc.Dispose();
            luaFunc = null;
        }

        lua.Dispose();
        lua = null;

#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived -= ShowTips;
#else
        Application.RegisterLogCallback(null);
#endif
    }

    int CallFunc()
    {        
        luaFunc.BeginPCall();                
        luaFunc.Push(123456);
        luaFunc.PCall();        
        int num = (int)luaFunc.CheckNumber();
        luaFunc.EndPCall();
        return num;                
    }
}

前面的lua状态实例化,后面的lua资源回收与前两节一模一样,区别就是中间的几种lua方法调用方式,使用过程中用那种都一样,但是性能上略有差别。
如上代码中看似四种,其实只有三种。
调用lua类中方法一:
Invoke:
luaFunc = lua.GetFunction(“test.luaFunc”);
int num = luaFunc.Invoke<int, int>(123456);
或者
int num = lua.Invoke<int, int>(“test.luaFunc”, 123456, true);

这种方式调用简单,但是调用过程中会有gc性能损耗,小量调用还是可以的;还有一个特殊的约束,就是最多支持9个参数1个返回值,不是不能用,自己评估就行。

调用lua类中方法二:
ToDelegate:
Func<int, int> Func = luaFunc.ToDelegate<Func<int, int>>();
num = Func(123456);

向LuaFunction中添加委托,利用委托实现函数返回,但是作者只封装了Func委托,其他的得自己去封装;也会有GC损耗,需要自己去优化;在一般使用情况下还是少用此方法,和上面一样有参数返回值限制。

调用lua类中方法三:
CallFunc:
luaFunc = lua.GetFunction(“test.luaFunc”);
int CallFunc()
{
luaFunc.BeginPCall();
luaFunc.Push(123456);
luaFunc.PCall();
int num = (int)luaFunc.CheckNumber();
luaFunc.EndPCall();
return num;
}
num = CallFunc();
调用方式略为麻烦,但是好在没有GC性能损耗,推荐使用。参数和返回值个数上还是有限制,最多6个参数1个返回值。

调用方式都会有参数和返回值限制,但是也不是上面大问题,用类或者结构体做参不就没啥问题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值