Exemplo n.º 1
0
        public static int AddRef(GameObject go)
        {
            PoolTicket ticket = go.AssertGetComponent <PoolTicket>();

            ticket.RefCnt++;
            return(ticket.RefCnt);
        }
Exemplo n.º 2
0
        protected override void _OnStart()
        {
            base._OnStart();

            PoolTicket ticket = this.AssertGetComponent <PoolTicket>();

            ticket.Pool = PrefabPool.ForceGetPoolByPrefab(_pfSource);
        }
Exemplo n.º 3
0
        public static int DecRef(GameObject go)
        {
            PoolTicket ticket = go.AssertGetComponent <PoolTicket>();

            ticket.RefCnt--;

            if (ticket.RefCnt <= 0)
            {
                ticket.Despawn();
            }

            return(ticket.RefCnt);
        }
Exemplo n.º 4
0
        public GameObject Spawn()
        {
            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)
            {
                spawned      = Instantiate(m_prefab) as GameObject;
                spawned.name = m_prefab.name;

                m_cont.Add(spawned);
                //Misc.AddChild(gameObject, spawned); //commented-out: no meaning to change the parent relation, would cause OnTransformParentChanged\
                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);
            }


            // 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
            Misc.AddChild((Transform)null, spawned); //remove spawned from pool's GO,

            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);
        }
Exemplo n.º 5
0
        public void Despawn(object obj)
        {
            GameObject go = obj as GameObject;

            Dbg.Assert(go != null, "PrefabPool.Despawn: not a gameobject");

            int idx = m_cont.IndexOf(go);

            if (!m_bAllowDespawnExtObj)
            {
                Dbg.Assert(idx != -1, "PrefabPool.Despawn: cannot find given object in container");
            }
            else
            {
                if (idx == -1)
                {
                    m_cont.Add(go);
#if PREFABPOOL_LOG
                    Dbg.Log("PrefabPool.Despawn: an external object is despawned into pool \"{0}\", name: {1}", m_poolName, go.name);
#endif
                }
            }

            PoolTicket ticket = go.ForceGetComponent <PoolTicket>();
            ticket.Pool    = this;
            ticket.IsAvail = true;
            Dbg.Assert(ticket.RefCnt == 0, "PrefabPool.Despawn: the refCnt is {0}, should be zero: '{1}'", ticket.RefCnt, ticket.name);
            ticket.RefCnt = 0;

            PoolTicketOption option = go.GetComponent <PoolTicketOption>();

            if (option && option.broadcastDespawnMsg)
            {
                go.BroadcastMessage("OnDespawn", SendMessageOptions.DontRequireReceiver);
            }
            else
            {
                go.SendMessage("OnDespawn", SendMessageOptions.DontRequireReceiver);
            }

            go.SetActive(false);                  // it's important to deactivate before add to pool, BECAUSE OF NGUI

            Misc.AddChild(gameObject, go, false); //use false because we want to ensure the world-pos/scale/rot not changed after add to the pool
        }
Exemplo n.º 6
0
        public static void DespawnPrefab(GameObject go, bool requireTicket = false)
        {
            PoolTicket ticket = go.GetComponent <PoolTicket>();

            if (ticket != null)
            {
                ticket.Despawn();
            }
            else
            {
                if (requireTicket)
                {
                    Dbg.LogWarn("PrefabPool.DespawnPrefab: cannot find PoolTicket when require it: '{0}'", go.name);
                }
                else
                {
                    PrefabPool pool = ForceGetPoolByPrefab(go);
                    pool.Despawn(go);
                }
            }
        }
Exemplo n.º 7
0
        public static void SubscribeOnDespawn(GameObject go, Action <GameObject> cb)
        {
            PoolTicket ticket = go.GetComponent <PoolTicket>();

            ticket.SubscribeOnDespawn(cb);
        }
Exemplo n.º 8
0
        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);
        }