예제 #1
0
 //            调试代码
 private void printClassLines(List <string> lines)
 {
     foreach (var line in lines)
     {
         BTLog.Error("line:{0}", line);
     }
 }
예제 #2
0
        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));
        }
예제 #3
0
        private void createLuaFieldByTrans(Transform trans, List <string> classDesc)
        {
            BTLog.Debug("createLuaFieldByTrans:{0}", trans.name);
            var numChildren = trans.childCount;
            var nameList    = new List <string>();

            for (var i = 0; i < numChildren; i++)
            {
                var child     = trans.GetChild(i);
                var childName = child.name;
                if (nameList.Contains(childName))
                {
                    throw new Exception(string.Format("组件命名重复:{0}", childName));
                }
                nameList.Add(childName);
                var suffix = Utils.GetSuffixOfGoName(childName);
//                如果不是合法后缀,则直接进入下一级,继续生成子go的fields
                if (!Utils.IsValidSuffix(suffix))
                {
                    createLuaFieldByTrans(child, classDesc);
                    continue;
                }
                classDesc.Insert(classDesc.Count - 1, string.Format("---@field {0} {1}", childName, Utils.GetTypeNameByComponentSuffix(suffix, child)));
//                _Doc不用对其子go生成field
                if (suffix != "_Doc")
                {
                    createLuaFieldByTrans(child, classDesc);
                }
            }
        }
예제 #4
0
//        该函数在调用前,必须保证当前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);
        }
예제 #5
0
파일: CSBridge.cs 프로젝트: ukyohpq/tolua
        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);
            }
        }
예제 #6
0
파일: Utils.cs 프로젝트: ukyohpq/tolua
        public static Type GetTypeByComponentSuffix(string suffix)
        {
            switch (suffix)
            {
            case ComponentSuffix.Text:
                return(typeof(UnityEngine.UI.Text));

            case ComponentSuffix.Button:
                return(typeof(Framework.UI.Button));

            default:
                BTLog.Warning("未定义的后缀名");
                return(null);
            }
        }
예제 #7
0
파일: MainGame.cs 프로젝트: ukyohpq/tolua
    private void Awake()
    {
        if (ins == null)
        {
            ins = this;
        }
        CreateUIStage();
        luaState = new LuaState();
        OpenLibs();
        luaState.LuaSetTop(0);
        Bind();
        LoadLuaFiles();
#if UNITY_EDITOR && !LOADFROM_BUNDLE
        BTLog.Debug("open HotFixLua");
        var hot = this.gameObject.AddComponent <HotFixLua>();
        hot.LuaPath = "/Lua/";
#endif
    }
예제 #8
0
        private void BindFieldsOnTrans(Transform trans, int topIdx)
        {
            BTLog.Debug("BindFieldsOnTrans trans:{0}", trans.name);
            var luaState    = MainGame.Ins.LuaState;
            var numChildren = trans.childCount;

            for (int i = 0; i < numChildren; i++)
            {
                var child     = trans.GetChild(i);
                var childName = child.name;
                if (childName == "")
                {
                    continue;
                }
                var suffix = Utils.GetSuffixOfGoName(childName);
//                如果不是合法后缀,则直接进入下一级,检测子go有没有需要绑定的
                if (!Utils.IsValidSuffix(suffix))
                {
                    BindFieldsOnTrans(child, topIdx);
                    continue;
                }
//                对Doc进行特殊处理,这个不能直接绑定cs组件,需要创建一个lua对象,然后进行绑定
                if (suffix == "_Doc")
                {
                    var childDoc = child.GetComponent <DocumentClass>();
                    childDoc.CreatePrefabAndBindLuaClass();
                    var childContextId = childDoc.GetContextId();
                    MainGame.Ins.GetPrefabLua(childContextId);
                    luaState.LuaSetField(topIdx, childName);
                }
                else
                {
                    BindFieldsOnTrans(child, topIdx);
                    var T = Utils.GetTypeByComponentSuffix(suffix);
                    if (T == null)
                    {
                        continue;
                    }
                    luaState.PushVariant(child.GetComponent(T));
                    luaState.LuaSetField(topIdx, childName);
                }
                BTLog.Debug("bind {0}. name:{1} childName:{2}", suffix, trans.name, childName);
            }
        }
예제 #9
0
파일: MainGame.cs 프로젝트: ukyohpq/tolua
 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);
 }
예제 #10
0
//        通过在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();
        }
예제 #11
0
파일: Utils.cs 프로젝트: ukyohpq/tolua
        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);
            }
        }