方法调用
_________________________________________________________________________________
源码:
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;
}
}
————————————————————————————————————————————————————————
原文输出:
————————————————————————————————————————————
代码看上去是方法的4种调用方式,尝试修改下试试:
//Get the function object
luaFunc = lua.GetFunction("test.luaFunc");
if (luaFunc != null)
{
//修改参数
int num = luaFunc.Invoke<int, int>(1234);
Debugger.Log("generic call return: {0}", num);
//方法主体在下方,不进行修改
num = CallFunc();
Debugger.Log("expansion call return: {0}", num);
//通过委托 获取到toLua方法 修改参数
Func<int, int> Func = luaFunc.ToDelegate<Func<int, int>>();
num = Func(12345);
Debugger.Log("Delegate call return: {0}", num);
//修改参数
num = lua.Invoke<int, int>("test.luaFunc", 1234567, true);
Debugger.Log("luastate call return: {0}", num);
}
修改之后结果按猜想的一样进行了变动,看了下底层机制<int,int> 前一个为形参类型,后一个为方法返回值类型