コード例 #1
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static GameObject SpawnPrefab(GameObject prefab, SpawnData data)
        {
            PrefabPool pool = ForceGetPoolByPrefab(prefab);
            GameObject go   = pool.Spawn(data);

            return(go);
        }
コード例 #2
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Transform tr)
        {
            SpawnData ret = new SpawnData();

            ret.tr    = tr;
            ret.flags = Tr;
            return(ret);
        }
コード例 #3
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Quaternion r)
        {
            SpawnData ret = new SpawnData();

            ret.rot   = r;
            ret.flags = Rot;
            return(ret);
        }
コード例 #4
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Vector3 p)
        {
            SpawnData ret = new SpawnData();

            ret.pos   = p;
            ret.flags = Pos;
            return(ret);
        }
コード例 #5
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Vector3 p, Transform tr)
        {
            SpawnData ret = new SpawnData();

            ret.pos   = p;
            ret.tr    = tr;
            ret.flags = Pos | Tr;
            return(ret);
        }
コード例 #6
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Vector3 p, Quaternion r)
        {
            SpawnData ret = new SpawnData();

            ret.pos   = p;
            ret.rot   = r;
            ret.flags = PR;
            return(ret);
        }
コード例 #7
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public static SpawnData Create(Vector3 p, Quaternion r, Transform tr)
        {
            SpawnData ret = new SpawnData();

            ret.pos   = p;
            ret.rot   = r;
            ret.tr    = tr;
            ret.flags = PR | Tr;
            return(ret);
        }
コード例 #8
0
ファイル: PrefabPool.cs プロジェクト: Vyvyan/carriage
        public GameObject Spawn(SpawnData data)
        {
            Dbg.Assert(m_prefab != null, "PrefabPool.Spawn: prefab is null");
            Dbg.Assert(m_poolName != null, "PrefabPool.Spawn: poolName is null");

            GameObject spawned = _FindInactiveGO();

            if (spawned == null)
            {
                var pfTr = m_prefab.transform;
                switch (data.flags)
                {
                case SpawnData.None: spawned = (GameObject)Instantiate(m_prefab, pfTr.position, pfTr.rotation); break;

                case SpawnData.Pos: spawned = (GameObject)Instantiate(m_prefab, data.pos, pfTr.rotation); break;

                case SpawnData.Rot: spawned = (GameObject)Instantiate(m_prefab, pfTr.position, data.rot); break;

                case SpawnData.PR: spawned = (GameObject)Instantiate(m_prefab, data.pos, data.rot); break;

                case SpawnData.Tr: spawned = (GameObject)Instantiate(m_prefab, pfTr.position, pfTr.rotation); break;

                case SpawnData.Tr | SpawnData.Pos: spawned = (GameObject)Instantiate(m_prefab, data.pos, pfTr.rotation); break;

                case SpawnData.Tr | SpawnData.Pos | SpawnData.Rot: spawned = (GameObject)Instantiate(m_prefab, data.pos, data.rot); break;

                default: Dbg.LogErr("PrefabPool.Spawn: unexpected flags: {0}", data.flags); break;
                }

                spawned.name = m_prefab.name;

                m_cont.Add(spawned);

                if (m_warnSize > 0 && m_cont.Count >= m_warnSize)
                {
                    Dbg.LogWarn("PrefabPool.Spawn: pool {0} has {1} elements, limit is {2}", m_poolName, m_cont.Count, m_warnSize);
                }

                //Dbg.Log("pool {0} instantiated a {1}, m_cont: {2}", m_poolName, m_prefab.name, m_cont.Count);
            }
            else
            {
                var tr = spawned.transform;
                if (data.HasPos())
                {
                    tr.position = data.pos;
                }
                if (data.HasRot())
                {
                    tr.rotation = data.rot;
                }
            }


            // NOTE: it's important to remove from pool BEFORE activating it
            // as NGUI has a petty behavior that would move any pool that has a ACTIVE UI element to be a child of UIRoot
            if (data.HasTr())
            {
                Misc.AddChild(data.tr, spawned);
            }
            else
            {
                Misc.AddChild((Transform)null, spawned);
            }

            spawned.SetActive(true); //set it to in use

            var option = spawned.GetComponent <PoolTicketOption>();

            if (option && option.broadcastSpawnMsg)
            {
                spawned.BroadcastMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
            }
            else
            {
                spawned.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver); //SendMsg won't send to inactive obj
            }

            // get PoolTicket, fill in this pool so it can be despawn from the object
            PoolTicket ticket = spawned.ForceGetComponent <PoolTicket>();

            ticket.Pool    = this;
            ticket.IsAvail = false;

            return(spawned);
        }