// 调试代码 private void printClassLines(List <string> lines) { foreach (var line in lines) { BTLog.Error("line:{0}", line); } }
public void AddEventListener(string eventName, LuaTable self, LuaFunction handler) { if (handler == null) { BTLog.Error("can not AddEventListener with null handler"); return; } List <LuaCallback> dic; if (!handlerMap.ContainsKey(eventName)) { dic = new List <LuaCallback>(); handlerMap[eventName] = dic; } else { dic = handlerMap[eventName]; } foreach (LuaCallback cb in dic) { if (cb.self == self && cb.handler == handler) { return; } } dic.Add(new LuaCallback(self, handler)); }
// 该函数在调用前,必须保证当前lua栈顶有一个lua的Prefab对象。也就是说,栈不为空! private void BindLuaClass() { var luaState = MainGame.Ins.LuaState; if (luaState.LuaIsNil(-1)) { BTLog.Error("该函数在调用前必须保证lua栈顶上有一个lua的Prefab对象"); return; } // TODO 这里考虑有没有必要把这个gameObject传给lua luaState.PushVariant(gameObject); luaState.LuaSetField(-2, "gameObject"); BindFieldsOnTrans(transform, luaState.LuaGetTop()); // 完成绑定之后,广播complete事件 luaState.LuaGetField(-1, "DispatchMessage"); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(1); BTLog.Warning("Prefab Lua must has Method:DispatchMessage"); return; } luaState.LuaInsert(-2); luaState.Push("COMPLETE"); luaState.LuaCall(2, 0); }
public static void LoadAsset() { for (int i = 0; i < 10; i++) { if (loaderContexts.Count == 0) { return; } var context = loaderContexts[0]; loaderContexts.RemoveAt(0); var path = context.Path; var contextID = context.ContextId; AssetPrototype asset; if (!AssetDict.TryGetValue(path, out asset)) { var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path); asset = new AssetPrototype(prefab); AssetDict.Add(path, asset); } if (asset == null) { BTLog.Error("can not find prefab in path:{0}", path); return; } var go = asset.GetGameObject(); MainGame.Ins.AddChild2Stage(go); go.transform.localPosition = Vector3.zero; if (contextPrefabDic.ContainsKey(contextID)) { BTLog.Error("contextPrefabDic contains key:{0}", contextID); return; } else if (contextPrefabDic.ContainsValue(go)) { BTLog.Error("contextPrefabDic contains value:{0}", go.name); return; } contextPrefabDic[contextID] = go; var docu = go.GetComponent <DocumentClass>(); if (docu == null) { throw new Exception("must has Component DocumentClass!"); } docu.SetContextId(contextID); } }
public void GetPrefabLua(int contextId) { luaState.LuaGetGlobal("prefabIDMap"); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(1); BTLog.Error("can nof find global table prefabIDMap"); return; } luaState.Push(contextId); luaState.LuaGetTable(-2); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(2); BTLog.Error(string.Format("load prefab document but can not find contextID:{0}", contextId)); return; } luaState.LuaReplace(-2); }
// 通过在cs端创建lua的Prefab对象进行绑定 private void CreatePrefabAndBindLuaClass() { var luaState = MainGame.Ins.LuaState; luaState.LuaGetGlobal("getPrefabID"); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(1); BTLog.Error("can not find lua function getPrefabID"); return; } var className = Utils.MakeClassName(LuaClass); luaState.LuaGetGlobal(className); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(2); BTLog.Error("can not find lua class:{0}", LuaClass); return; } luaState.LuaGetField(-1, "New"); if (luaState.LuaIsNil(-1)) { luaState.LuaPop(3); BTLog.Error("can not find constructor for lua class:{0}", LuaClass); return; } luaState.LuaCall(0, 1); // 删除luaclass luaState.LuaRemove(-2); luaState.LuaDup(); //将dup出来的prefab实例放到栈底备用 luaState.LuaInsert(-3); //call getPrefabID获取contextId luaState.LuaCall(1, 1); contextId = luaState.LuaToInteger(-1); luaState.LuaPop(1); // var prefab = luaState.ToVariant(-1) as LuaTable; BindLuaClass(); }
public static string GetTypeNameByComponentSuffix(string suffix, Transform trans) { switch (suffix) { case ComponentSuffix.Text: return("UnityEngine.UI.Text"); case ComponentSuffix.Button: return("Framework.UI.Button"); case ComponentSuffix.Doc: var doc = trans.GetComponent <DocumentClass>(); if (doc == null) { BTLog.Error("以Doc为后缀的名称的组件,必须拥有DocumentClass组件"); return(""); } return(doc.GetLuaClassName()); default: BTLog.Warning("未定义的后缀名"); return(null); } }