game.cs
using LuaInterface;
public class Game : MonoBehaviour {
private LuaState lua = null;
void Awake()
{
lua = new LuaState();
lua.Start();
string fullPath = Application.dataPath + "\\Lua/Snake";//lua脚本存放路径
lua.AddSearchPath(fullPath);
LuaBinder.Bind(lua);
}
void Start()
{
lua.DoFile("game.lua");
LuaToCs.DoLuaFunction(lua.GetFunction("GameStart"));
LuaToCs.DoLuaFunction(lua.GetFunction("SetForward"), forward);
snakeMoveSpeed = LuaToCs.GetLuaIntData(lua.GetFunction("GetSnakeMoveSpeed"));
}
}
LuaToCs.cs
public class LuaToCs{
public static void DoLuaFunction(LuaFunction func)
{
if (func != null)
{
func.BeginPCall();
func.PCall();
func.EndPCall();
}
else
{
Debug.LogError(string.Format("{0} is null!", func));
}
}
public static void DoLuaFunction(LuaFunction func, string str)
{
if (func != null)
{
func.BeginPCall();
func.Push(str);
func.PCall();
func.EndPCall();
}
else
{
Debug.LogError(string.Format("{0} is null!", func));
}
}
public static int GetLuaIntData(LuaFunction func)
{
int temp = -1;
if (func != null)
{
func.BeginPCall();
func.PCall();
temp = (int)func.CheckNumber();
func.EndPCall();
}
else
{
Debug.LogError(string.Format("{0} is null!", func));
}
return temp;
}
}