internal PoolableObject PreloadInstance() { _ValidatePooledObjectDataContainer(); PoolableObject poolObj = _NewPooledInstance(null, null); poolObj._wasPreloaded = true; _SetAvailable(poolObj, true); return(poolObj); }
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); }
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); } }
/// <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); }
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); } } }
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); } }
// **************************************************************************************************/ // 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)); } }
/// <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 } }
/// <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); }
/// <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)); } }
/// <summary> /// Resets the reference to <c>null</c>. /// </summary> public void Reset() { _pooledObj = null; _objComponent = null; _initialUsageCount = 0; }
/// <summary> /// Initializes a new instance of the <see cref="PoolableReference<T>"/> class from /// a given <see cref="PoolableReference<T>"/>. /// </summary> /// <param name="poolableReference">The poolable reference.</param> public PoolableReference(PoolableReference <T> poolableReference) { _objComponent = poolableReference._objComponent; _pooledObj = poolableReference._pooledObj; _initialUsageCount = poolableReference._initialUsageCount; }
internal void Remove(PoolableObject poolObj) { _pool.Remove(poolObj); }
public ObjectPool(GameObject prefab) { _prefabPoolObj = prefab.GetComponent <PoolableObject>(); }