前面几章说到了怎么执行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个返回值。
调用方式都会有参数和返回值限制,但是也不是上面大问题,用类或者结构体做参不就没啥问题了。