public static object[] CallTableFunc(this LuaTable curTable, string strFuncName, params object[] pParams) { LuaState curLuaState = curTable.GetLuaState(); int dwTop = curLuaState.LuaGetTop(); object[] pRtnObjs = null; try { if (curTable.BeginCall(strFuncName, dwTop)) { int dwParamNum = 0; //压入self { curLuaState.PushGeneric(curTable); ++dwParamNum; } //压入参数 if (pParams != null) { for (int i = 0; i < pParams.Length; ++i) { curLuaState.PushGeneric(pParams[i]); ++dwParamNum; } } curLuaState.Call(dwParamNum, dwTop + 2, dwTop); //由于beginCall压入了一个table和trace,所以起始偏移+2 pRtnObjs = curLuaState.CheckObjects(dwTop + 2); } else { curLuaState.LuaSetTop(dwTop); throw new LuaException("call fail"); } curLuaState.LuaSetTop(dwTop); } catch (Exception e) { curLuaState.LuaSetTop(dwTop); throw e; } return(pRtnObjs); }
// Use this for initialization void Start() { if (luaState == null) { luaState = new LuaState(); luaState.Start(); luaState.LuaCheckStack(200); luaState.CheckTop(); LuaBinder.Bind(luaState); luaState.AddSearchPath(Application.dataPath + "\\Scripts/lua"); luaState.DoFile("lua_bridge.lua"); luaInit = luaState.GetFunction("lua_init"); luaUpdate = luaState.GetFunction("lua_update"); int _ref = luaInit.BeginPCall(); luaInit.PCall(); object[] tb = luaState.CheckObjects(_ref); luaInit.EndPCall(); luaRoot = tb[0] as LuaTable; } Init(); }
protected object[] CallMethod(string func, params object[] args) { if (self == null) { return(null); } LuaFunction lfunc = (LuaFunction)self[func]; if (lfunc == null) { return(null); } //等价于lua语句: self:func(...) int oldTop = lfunc.BeginPCall(); lfunc.Push(self); lfunc.PushArgs(args); lfunc.PCall(); object[] objs = luaState.CheckObjects(oldTop); lfunc.EndPCall(); return(objs); }