Example #1
0
        /// <summary>
        /// Despawns the specified object.
        /// </summary>
        /// <param name="obj">The object.</param>
        public void Despawn(GameObject obj)
        {
            PoolMember pm = obj.GetComponent <PoolMember>();

            if (null == pm)
            {
                Debug.LogError("Object " + obj + "wasn't spawned from a pool. " +
                               "Destroying instead...");
                Destroy(obj);
            }
            else
            {
                pm.Pool.Despawn(pm);
            }
        }
Example #2
0
        /// <summary>
        /// Spawns this pool's <see cref="_poolObject"/>
        /// with the corresponding parameters
        /// </summary>
        /// <param name="position">The position.</param>
        /// <param name="rotation">The rotation.</param>
        /// <param name="localScale">The local scale.</param>
        /// <returns></returns>
        public GameObject Spawn(Vector3 position    = default(Vector3),
                                Quaternion rotation = default(Quaternion),
                                Vector3?localScale  = null)
        {
            if (localScale == null)
            {
                localScale = new Vector3(1, 1, 1);
            }

            GameObject obj;
            PoolMember member = null;

            if (_actives.Count < _capacity)
            {
                if (_inactives.Count == 0)
                {
                    // the object isn't on the pool, thus it's instantiated
                    obj      = Instantiate(_poolObject);
                    obj.name = _poolObject.name + " (" + obj.GetInstanceID() + ")";
                    obj.transform.SetParent(transform);
                    // link with this object pool
                    member = obj.GetComponent <PoolMember>();

                    if (null == member)
                    {
                        obj.AddComponent <PoolMember>().Pool = this;
                    }
                    else
                    {
                        member.Pool = this;
                    }
                }
                else
                {
                    // grab the last inactive
                    obj = _inactives.Pop();
                }
            }
            else
            {
                obj = _actives.Dequeue();
            }

            // the expected obj has been destroyed somewhere else
            if (obj == null)
            {
                return(Spawn(position, rotation, localScale));
            }

            if (null != member)
            {
                member.OnSpawn();
            }
            else
            {
                obj.GetComponent <PoolMember>().OnSpawn();
            }

            // set transform parameters
            obj.transform.position   = position;
            obj.transform.rotation   = rotation;
            obj.transform.localScale = localScale.Value;
            obj.SetActive(true);
            _actives.Enqueue(obj);
            return(obj);
        }
Example #3
0
 /// <summary>
 /// Puts the despawned object back to the inactives stack
 /// and deactivates it's <see cref="GameObject"/>
 /// </summary>
 /// <param name="member">The member.</param>
 private void Despawn(PoolMember member)
 {
     member.gameObject.SetActive(false);
     member.OnDespawn();
     _inactives.Push(member.gameObject);
 }