Esempio n. 1
0
        // @desc GameObjectを実体化する
        // @decl GameObject instantiate(object)
        // @param object GameObject
        // @result GameObject
        // @sample local g = GameObject.instantiate(Resources.load_game_object("hoge"))
        private static int L_instantiate(ILuaState lua)
        {
            check_identifier(lua, 1, klassName);
            GameObject src = get_internal <GameObject>(lua, 1);
            GameObject obj = (GameObject)GameObject.Instantiate(src);

            Debug.Log(obj);
            LuaGameObjectCache cache = obj.GetComponent <LuaGameObjectCache>();

            //if (cache != null) Object.Destroy(cache);
            PushNew(lua, obj);
            return(1);
        }
Esempio n. 2
0
        // -- describing object oriented class in Lua
        // Test = {}
        // function Test:new()
        //   o = {}
        //   setmetatable(o, self)
        //   self.__index = self
        //   return o
        // end
        // function Test:hoge()
        //   return 3
        // end
        // NewTest = Test:new()
        // function NewTest:hoge()
        //   return 4
        // end
        public static int PushNew(ILuaState lua, GameObject obj)
        {
            LuaVM.Assert(lua, obj != null);
            LuaGameObjectCache cache = obj.GetComponent <LuaGameObjectCache>();

            if (cache != null && cache.index == LuaConstants.LUA_NOREF)
            {
                Object.Destroy(cache);
                cache = null;
            }
            if (cache == null)
            {
                cache = obj.AddComponent <LuaGameObjectCache>();
                // o = {}
                lua.NewTable();
                // setmetatable(o, self)
                //lua.PushValue(-2); // self
                lua.GetGlobal(klassName);
                lua.SetMetaTable(-2); // -> o
                // self.__index = self
                lua.GetMetaTable(-1);
                lua.SetField(-1, "__index");
                // return o
                set_internal <GameObject>(lua, -1, obj);
                set_boolean(lua, -1, "is_prefab", false);
                //
                LuaScriptBehaviour lsb = obj.GetComponent <LuaScriptBehaviour>();
                if (lsb != null)
                {
                    LuaComponentBehaviour.PushNew(lua, lsb);
                    lua.SetField(-2, "behaviour");
                }
                LuaComponentTransform.PushNew(lua, obj.transform);
                lua.SetField(-2, "transform");
                RectTransform rt = obj.GetComponent <RectTransform>();
                if (rt != null)
                {
                    LuaComponentRectTransform.PushNew(lua, rt);
                    lua.SetField(-2, "rect_transform");
                }
                LuaComponentUIText.PushNew(lua, obj);
                lua.SetField(-2, "ui_text");
                LuaComponentUIImage.PushNew(lua, obj);
                lua.SetField(-2, "ui_image");
                if (constructorHook != null)
                {
                    constructorHook(lua, obj);
                }
                lua.PushValue(-1);
                int refidx = lua.L_Ref(LuaDef.LUA_REGISTRYINDEX);
                cache.index    = refidx;
                cache.luaState = lua;
            }
            lua.RawGetI(LuaDef.LUA_REGISTRYINDEX, cache.index);
            //Debug.Log(string.Format("Find {0} {1}", objid, obj.name));
            if (get_internal <GameObject>(lua, -1) == null)
            {
                set_internal <GameObject>(lua, -1, obj);
            }
            return(1);
        }