001.在C#代码中,直接 写 Lua代码
void Awake()
{
LuaState lua = new LuaState();
lua.Start();
string luaScriptContent = @"print('在C#代码中,直接 写 Lua代码,start lua studio --> tolua');";
lua.DoString(luaScriptContent,"SudioHello.cs");
lua.CheckTop();
lua.Dispose();
lua = null;
}
002.读取外部文件,require一次,dofile可多次
//展示searchpath使用,require 与 dofile区别
//lua.DoFile("StudioScriptsFromFile.lua"); //dofile,要写文件后缀,直接执行这个代码,而且多次执行,每次触发就会被调用。
//lua.Require ("StudioScriptsFromFile"); //require加载只执行一次,如果加载过不会再调用。
public class StudioScriptsFromFile : MonoBehaviour
{
LuaState lua = null;
private string strLog = "";
void Start()
{
Application.logMessageReceived += Application_logMessageReceived;
lua = new LuaState();
lua.Start();
//绝对路径
string fullPath = Application.dataPath + @"\LuaFramework\ToLua\Examples\02_ScriptsFromFile";
lua.AddSearchPath(fullPath);//增加路径
}
/// <summary>
/// 接收log信息
/// </summary>
void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
strLog += condition;
strLog += "\r\n";
}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 1000, 100), strLog);
if (GUI.Button(new Rect(0, 110, 100, 150), "DoFile"))
{
strLog = "";
lua.DoFile("StudioScriptsFromFile.lua");
}
else if (GUI.Button(new Rect(230, 100, 100, 100), "Require"))
{
strLog = "";
lua.Require("StudioScriptsFromFile");
}
lua.Collect();
lua.CheckTop();
}
void OnApplicationQuit()
{
lua.Dispose();
lua = null;
Application.logMessageReceived -= Application_logMessageReceived;
}
}
03.使用lua的返回值 LuaState.Invoke比较直接
/// <summary>
/// 使用lua的返回值
/// </summary>
public class StudioCallLuaFunction : MonoBehaviour
{
private string luaScriptContent = @"
function luaFunction(num)
print('函数luaFunction--加1');
return num+1;
end
test={};
test.luaFunction=luaFunction
";
LuaFunction luaFunc = null;
LuaState lua = null;
string tips = null;
void Start()
{
Application.logMessageReceived += Application_logMessageReceived;
new LuaResLoader();
lua = new LuaState();
lua.Start();
DelegateFactory.Init();
lua.DoString(luaScriptContent);
luaFunc = lua.GetFunction("test.luaFunction");
if (luaFunc != null)
{
int num = luaFunc.Invoke<int, int>(123456); //第一个int是传入值,最后一个int是返回值
Debugger.Log("返回值,用 luaFunc.Invoke 调用----第一个int是传入值,第二个int是返回值 -->:" + num);
num = callFunc();
Debugger.Log("\r\n 自己的写的回调 ,然而跟invoke很相同-->:" + num);
Func<int, int> Func = luaFunc.ToDelegate<Func<int, int>>();
num = Func(123456);
Debugger.Log("\r\n 使用委托:" + num);
num = lua.Invoke<int, int>("test.luaFunction", 123456, true);
Debugger.Log("\r\n直接使用LuaState.Invoke ,中间少了LuaFunction这个类 -->:" + num);
}
lua.CheckTop();
Debugger.Log("\r\n 统上所言,使用LuaState.Invoke是最直观的");
}
//public R1 Invoke<T1, R1>(T1 arg1)
//{
// BeginPCall();
// PushGeneric(arg1);
// PCall();
// R1 ret1 = CheckValue<R1>();
// EndPCall();
// return ret1;
//}
private int callFunc()
{//这个回调,跟Invoke方法基本相同
luaFunc.BeginPCall();
luaFunc.Push(123456);
luaFunc.PCall();
int num = (int)luaFunc.CheckNumber();
luaFunc.EndPCall();
return num;
}
void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
tips += condition;
tips += "\r\n";
}
void OnGUI() {
GUI.Label(new Rect(0,10,600,800),tips);
}
void OnDestroy() {
if (luaFunc!=null)
{
luaFunc.Dispose();
luaFunc = null;
}
lua.Dispose();
lua = null;
Application.logMessageReceived -= Application_logMessageReceived;
}
}