Beispiel #1
0
            internal PoolableObject PreloadInstance()
            {
                _ValidatePooledObjectDataContainer();

                PoolableObject poolObj = _NewPooledInstance(null, null);

                poolObj._wasPreloaded = true;

                _SetAvailable(poolObj, true);

                return(poolObj);
            }
Beispiel #2
0
        static internal ObjectPool _GetPool(PoolableObject prefabPoolComponent)
        {
            ObjectPool pool;

            GameObject prefab = prefabPoolComponent.gameObject;

            if (!_pools.TryGetValue(prefab, out pool))
            {
                pool = new ObjectPool(prefab);
                _pools.Add(prefab, pool);
            }

            return(pool);
        }
Beispiel #3
0
            private void _RecursivelySetInactiveAndSendMessages(GameObject obj, PoolableObject parentPoolObj, bool recursiveCall)
            {
                // Create a local copy of all of the children before we potentially modify it
                // by removing a child PoolableObject by making a call to _SetAvailable()

                var objTransform = obj.transform;

                Transform[] children = new Transform[objTransform.childCount];

                for (int i = 0; i < objTransform.childCount; i++)
                {
                    children[i] = objTransform.GetChild(i);
                }

                //now recursively do the same for all children
                for (int i = 0; i < children.Length; i++)
                {
                    Transform child = children[i];

                    var poolableChild = child.gameObject.GetComponent <PoolableObject>();

                    if (poolableChild && poolableChild._myPool != null)   //if child is poolable itself it has to be detached and moved to the pool
                    {
                        _SetAvailable(poolableChild, true);
                    }
                    else
                    {
                        _RecursivelySetInactiveAndSendMessages(child.gameObject, parentPoolObj, true);
                    }
                }

                if (parentPoolObj.sendAwakeStartOnDestroyMessage)
                {
                    obj.SendMessage("OnDestroy", null, SendMessageOptions.DontRequireReceiver);
                }

                if (parentPoolObj.sendPoolableActivateDeactivateMessages)
                {
                    obj.SendMessage("OnPoolableObjectDeactivated", null, SendMessageOptions.DontRequireReceiver);
                }

#if UNITY_3_x
#else
                if (!recursiveCall)
#endif
                {
                    _SetActive(obj, false);
                }
            }
Beispiel #4
0
        /// <summary>
        /// Gets the reference to the script component, or <c>null</c> if the object was
        /// already destroyed or moved to the pool.
        /// </summary>
        /// <returns>
        /// The reference to <c>T</c> or null
        /// </returns>
        public T Get()
        {
            if (!_objComponent)
            {
                return(null);
            }

            if (_pooledObj)   // could be set to a none-poolable object
            {
                if (_pooledObj._usageCount != _initialUsageCount || _pooledObj._isAvailableForPooling)
                {
                    _objComponent = null;
                    _pooledObj    = null;
                    return(null);
                }
            }
            return(( T )_objComponent);
        }
Beispiel #5
0
        private static void _BroadcastMessageToAllChildren(GameObject go, string message)
        {
            Transform[] children = new Transform[go.transform.childCount];   // save child array, as it may change

            for (int i = 0; i < go.transform.childCount; i++)
            {
                children[i] = go.transform.GetChild(i);
            }

            //now recursively do the same for all children
            for (int i = 0; i < children.Length; i++)
            {
                if (children[i].GetComponent <PoolableObject>() == null)    // if child object is PoolableObject then don't broadcast
                {
                    PoolableObject._BroadcastMessageToGameObject(children[i].gameObject, message);
                }
            }
        }
Beispiel #6
0
            private void _SetActiveAndSendMessages(GameObject obj, PoolableObject parentPoolObj)
            {
                _SetActive(obj, true);

                if (parentPoolObj.sendAwakeStartOnDestroyMessage)
                {
                    obj.BroadcastMessage("Awake", null, SendMessageOptions.DontRequireReceiver);

                    if (_GetActive(obj)
                        &&                                    // Awake could deactivate object
                        parentPoolObj._wasStartCalledByUnity) // for preloaded objects Unity will call Start
                    {
                        obj.BroadcastMessage("Start", null, SendMessageOptions.DontRequireReceiver);
                    }
                }

                if (parentPoolObj.sendPoolableActivateDeactivateMessages)
                {
                    obj.BroadcastMessage("OnPoolableObjectActivated", null, SendMessageOptions.DontRequireReceiver);
                }
            }
Beispiel #7
0
        // **************************************************************************************************/
        //          public functions
        // **************************************************************************************************/

        /// <summary>
        /// Retrieves an instance of the specified prefab. Either returns a new instance or it claims an instance
        /// from the pool.
        /// </summary>
        /// <param name="prefab">The prefab to be instantiated.</param>
        /// <returns>
        /// An instance of the prefab.
        /// </returns>
        /// <remarks>
        /// Can be used on none-poolable objects as well. It is good practice to use <c>ObjectPoolController.Instantiate</c>
        /// whenever you may possibly make your prefab poolable in the future.
        /// </remarks>
        /// <seealso cref="Destroy(GameObject)"/>
        static public GameObject Instantiate(GameObject prefab)
        {
            PoolableObject prefabPool = prefab.GetComponent <PoolableObject>();

            if (prefabPool == null)
            {
                //Debug.LogWarning( "Object " + prefab.name + " not poolable " );
                return(( GameObject )GameObject.Instantiate(prefab));   // prefab not pooled, instantiate normally
            }

            GameObject go = _GetPool(prefabPool).GetPooledInstance(null, null);

            if (go)
            {
                return(go);
            }
            else // pool is full
            {
                return(InstantiateWithoutPool(prefab));
            }
        }
Beispiel #8
0
        /// <summary>
        /// Destroys the specified game object, respectively sets the object inactive and adds it to the pool.
        /// </summary>
        /// <param name="obj">The game object.</param>
        /// <remarks>
        /// Can be used on none-poolable objects as well. It is good practice to use <c>ObjectPoolController.Destroy</c>
        /// whenever you may possibly make your prefab poolable in the future. <para/>
        /// Must also be used on none-poolable objects with poolable child objects so the poolable child objects are correctly
        /// moved to the pool.
        /// </remarks>
        /// <seealso cref="Instantiate(GameObject)"/>
        static public void Destroy(GameObject obj)   // destroys poolable and none-poolable objects. Destroys poolable children correctly
        {
            PoolableObject poolObj = obj.GetComponent <PoolableObject>();

            if (poolObj == null)
            {
                _DetachChildrenAndDestroy(obj.transform); // child objects may be poolable
                GameObject.Destroy(obj);                  // prefab not pooled, destroy normally
                return;
            }
            if (poolObj._myPool != null)
            {
                poolObj._myPool._SetAvailable(poolObj, true);
            }
            else
            {
                if (!poolObj._createdWithPoolController)
                {
                    Debug.LogWarning("Poolable object " + obj.name + " not created with ObjectPoolController");
                }
                GameObject.Destroy(obj);   // prefab not pooled, destroy normally
            }
        }
Beispiel #9
0
        /// <summary>
        /// Instantiates the specified prefab without using pooling.
        /// from the pool.
        /// </summary>
        /// <param name="prefab">The prefab to be instantiated.</param>
        /// <param name="position">The position in world coordinates.</param>
        /// <param name="quaternion">The rotation quaternion.</param>
        /// <returns>
        /// An instance of the prefab.
        /// </returns>
        /// <remarks>
        /// If the prefab is poolable, the <see cref="PoolableObject"/> component will be removed.
        /// This way no warning is generated that a poolable object was created without pooling.
        /// </remarks>
        static public GameObject InstantiateWithoutPool(GameObject prefab, Vector3 position, Quaternion quaternion)
        {
            GameObject go;

            _isDuringInstantiate = true;
            go = ( GameObject )GameObject.Instantiate(prefab, position, quaternion);    // prefab not pooled, instantiate normally
            _isDuringInstantiate = false;

            PoolableObject pool = go.GetComponent <PoolableObject>();

            if (pool)
            {
                if (pool.doNotDestroyOnLoad)
                {
                    GameObject.DontDestroyOnLoad(go);
                }

                pool._createdWithPoolController = true; // so no warning is displayed if object gets ObjectPoolCOntroller.Destroy()-ed before the component actually gets removed
                Component.Destroy(pool);
            }

            return(go);
        }
Beispiel #10
0
        /// <summary>
        /// Retrieves an instance of the specified prefab. Either returns a new instance or it claims an instance
        /// from the pool.
        /// </summary>
        /// <param name="prefab">The prefab to be instantiated.</param>
        /// <param name="position">The position in world coordinates.</param>
        /// <param name="quaternion">The rotation quaternion.</param>
        /// <returns>
        /// An instance of the prefab.
        /// </returns>
        /// <remarks>
        /// Can be used on none-poolable objects as well. It is good practice to use <c>ObjectPoolController.Instantiate</c>
        /// whenever you may possibly make your prefab poolable in the future.
        /// </remarks>
        /// <seealso cref="Destroy(GameObject)"/>
        static public GameObject Instantiate(GameObject prefab, Vector3 position, Quaternion quaternion)
        {
            PoolableObject prefabPool = prefab.GetComponent <PoolableObject>();

            if (prefabPool == null)
            {
                // no warning displayed by design because this allows to decide later if the object will be poolable or not
                // Debug.LogWarning( "Object " + prefab.name + " not poolable ");
                return(( GameObject )GameObject.Instantiate(prefab, position, quaternion));   // prefab not pooled, instantiate normally
            }

            GameObject go = _GetPool(prefabPool).GetPooledInstance(position, quaternion);

            if (go)
            {
                return(go);
            }
            else // pool is full
            {
                //Debug.LogWarning( "Pool Full" );
                return(InstantiateWithoutPool(prefab, position, quaternion));
            }
        }
Beispiel #11
0
 /// <summary>
 /// Resets the reference to <c>null</c>.
 /// </summary>
 public void Reset()
 {
     _pooledObj         = null;
     _objComponent      = null;
     _initialUsageCount = 0;
 }
Beispiel #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PoolableReference&lt;T&gt;"/> class from
 /// a given <see cref="PoolableReference&lt;T&gt;"/>.
 /// </summary>
 /// <param name="poolableReference">The poolable reference.</param>
 public PoolableReference(PoolableReference <T> poolableReference)
 {
     _objComponent      = poolableReference._objComponent;
     _pooledObj         = poolableReference._pooledObj;
     _initialUsageCount = poolableReference._initialUsageCount;
 }
Beispiel #13
0
 internal void Remove(PoolableObject poolObj)
 {
     _pool.Remove(poolObj);
 }
Beispiel #14
0
 public ObjectPool(GameObject prefab)
 {
     _prefabPoolObj = prefab.GetComponent <PoolableObject>();
 }