예제 #1
0
        void Clear()
        {
            if (onUpdateAssistant != null)
            {
                GameObject.Destroy(onUpdateAssistant);
            }
            if (onFixedUpdateAssistant != null)
            {
                GameObject.Destroy(onFixedUpdateAssistant);
            }
            if (onLateUpdateAssistant != null)
            {
                GameObject.Destroy(onLateUpdateAssistant);
            }

            if (LuaInstance != null)
            {
                var delWith = LuaInstance.Get <Action <LuaTable> >("delWith");
                delWith?.Invoke(LuaInstance);

                LuaInstance.Set("owner", false);
                LuaInstance.Set("gameObject", false);
            }

            onAwakeFunc            = null;
            onStartFunc            = null;
            onUpdateAssistant      = null;
            onFixedUpdateAssistant = null;
            onLateUpdateAssistant  = null;
            onEnableFunc           = null;
            onDisableFunc          = null;

            mLuaInstance = null;
        }
예제 #2
0
        public void SetTable(LuaTable luaInstance)
        {
            Clear();

            mLuaInstance = luaInstance;

            if (LuaInstance != null)
            {
                //注入自己
                LuaInstance.Set("owner", this);
                LuaInstance.Set("gameObject", gameObject);

                //其它参数注入
                LuaTable defineTable;
                LuaInstance.Get("_defineList", out defineTable);
                if (defineTable != null)
                {
                    Dictionary <string, Type> infoDict = new Dictionary <string, Type>();
                    string infoName;
                    Type   infoType;
                    defineTable.ForEach <int, LuaTable>((index, infoTable) =>
                    {
                        infoTable.Get("name", out infoName);
                        infoTable.Get("type", out infoType);
                        infoDict.Add(infoName, infoType);
                    });
                    //注入object类型对象
                    foreach (var serializedValue in SerializedObjValues)
                    {
                        //看了下set的代码,没有注册任何object类型的pushfunc,所以最后调用的都是pushany,
                        //不用担心类型错误问题
                        if (infoDict.ContainsKey(serializedValue.key) && serializedValue.value != null)
                        {
                            LuaInstance.Set(serializedValue.key, serializedValue.value);
                        }
                    }
                    //注入其他类型
                    foreach (var serializedValue in SerializedValues)
                    {
                        if (infoDict.ContainsKey(serializedValue.key))
                        {
                            LuaInstance.Set(serializedValue.key, JsonToValue(serializedValue.jsonStr, infoDict[serializedValue.key]));
                        }
                    }
                }

                //回调注册
                var newWith = LuaInstance.Get <Action <LuaTable> >("newWith");
                newWith?.Invoke(LuaInstance);

                onAwakeFunc   = LuaInstance.Get <Action <LuaTable> >("awake");
                onStartFunc   = LuaInstance.Get <Action <LuaTable> >("start");
                onEnableFunc  = LuaInstance.Get <Action <LuaTable> >("onEnable");
                onDisableFunc = LuaInstance.Get <Action <LuaTable> >("onDisable");
                onDestroyFunc = LuaInstance.Get <Action <LuaTable> >("onDestroy");

                var onUpdateFunc = LuaInstance.Get <Action <LuaTable> >("update");
                if (onUpdateFunc != null)
                {
                    onUpdateAssistant = gameObject.AddComponent <LuaUpdateAssistant>();
                    onUpdateAssistant.luaBehaviour = this;
                    onUpdateAssistant.assistFunc   = onUpdateFunc;
                }

                var onFixedUpdateFunc = LuaInstance.Get <Action <LuaTable> >("fixedUpdate");
                if (onFixedUpdateFunc != null)
                {
                    onFixedUpdateAssistant = gameObject.AddComponent <LuaFixedUpdateAssistant>();
                    onFixedUpdateAssistant.luaBehaviour = this;
                    onFixedUpdateAssistant.assistFunc   = onFixedUpdateFunc;
                }

                var onLateUpdateFunc = LuaInstance.Get <Action <LuaTable> >("lateUpdate");
                if (onLateUpdateFunc != null)
                {
                    onLateUpdateAssistant = gameObject.AddComponent <LuaLateUpdateAssistant>();
                    onLateUpdateAssistant.luaBehaviour = this;
                    onLateUpdateAssistant.assistFunc   = onFixedUpdateFunc;
                }
            }
        }