/// <summary> /// This is how pool object is instantiated by default /// Override it if you want to add some changes to the instantiation procedure /// </summary> /// <returns>Instantiated object of prefab</returns> protected virtual T UnityInstantiate <T>(GenericPool pool) where T : MonoBehaviour { GameObject pooledObject = Instantiate(pool.Prefab); #if UNITY_EDITOR && DEBUG_ZPOOLING pooledObject.name = pool.Prefab.name + " " + pool.TotalSpawned; pool.TotalSpawned++; #endif // Adding to the pool as instance of generic pool element if (pooledObject.TryGetComponent(out IGenericPoolElement element)) { pool.AddToPool(element); OnUnityInstantiatePostProcess(element); } #if DEBUG else { Debug.LogError("Unable to find component (" + typeof(T) + ") on generic pool element instance - " + pooledObject); } #endif return(element as T); }
/// <summary> /// Obtains available element or instantiates a new one /// </summary> protected virtual T ObtainElement <T>(GenericPool pool) where T : MonoBehaviour { if (pool.HasAvailableElements) { return(pool.PoolFirst <T>()); } // Instantiating a new one return(UnityInstantiate <T>(pool)); }
/// <summary> /// Instantiates an actual pool object to contain data about the pool /// </summary> protected GenericPool InstantiatePool(GameObject prefab) { GenericPool pool = new GenericPool { Prefab = prefab }; PoolerLibrary.Add(prefab, pool); HashReference.Add(pool.GetHashCode(), pool); return(pool); }