/// <summary> /// Registers a pool in the pool manager. /// </summary> /// <param name="poolBhv"></param> public static void RegisterPool(PoolBehaviour poolBhv) { if (poolBhv.Prefab) { s_instantiatePoolLookupDic[poolBhv.Prefab] = poolBhv; } }
/// <summary> /// Gets the number of available instances are in the pool cache. /// </summary> /// <param name="original"></param> /// <returns></returns> public static int GetCacheSize(Object original) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { return(poolBhv.CacheSize); } return(0); }
/// <summary> /// Gets how many active instances belonging to the prefab are there. /// </summary> /// <param name="original"></param> /// <returns></returns> public static int GetActiveInstances(Object original) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { return(poolBhv.ActiveCount); } return(0); }
/// <summary> /// Fix null references in the Pools when an intance was destroyed using Object.Destroy instead using the PoolManager.Destroy or this.PoolDestroy methods. /// </summary> /// <param name="warningLogEnabled"></param> public static void RemoveNullGameObjects(bool warningLogEnabled = true) { var iter = s_instantiatePoolLookupDic.GetEnumerator(); while (iter.MoveNext()) { PoolBehaviour objFactoryBhv = iter.Current.Value; if (objFactoryBhv) { objFactoryBhv.RemoveNullGameObjects(warningLogEnabled); } } }
/// <summary> /// Manually creates a Pool for a prefab. /// </summary> /// <param name="prefab"></param> /// <returns></returns> public static PoolBehaviour CreatePool(GameObject prefab) { CheckInitialize(); GameObject obj = new GameObject("Pool: " + prefab.name); obj.transform.SetParent(s_poolManagerObj.transform); PoolBehaviour bhv = obj.AddComponent <PoolBehaviour>(); bhv.Prefab = prefab; bhv.Initialize(); return(bhv); }
/// <summary> /// Destroys all the instances in all the Pools. /// </summary> public static void DestroyAll() { var iter = s_instantiatePoolLookupDic.GetEnumerator(); while (iter.MoveNext()) { PoolBehaviour objFactoryBhv = iter.Current.Value; if (objFactoryBhv) { objFactoryBhv.DestroyAll(); } } }
/// <summary> /// Generates a cache of instances for the prefab (creating a new pool if needed). /// </summary> /// <param name="original"></param> /// <param name="count"></param> public static void PreCacheInstances(Object original, int count) { count = Mathf.Max(0, count); if (original is Component || original is GameObject) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { poolBhv.PreCacheInstances(count); } } }
/// <summary> /// Finds and return a Pool using the prefab or creates a new one if needed. /// </summary> /// <param name="prefab"></param> /// <returns></returns> public static PoolBehaviour FindOrCreatePool(GameObject prefab) { if (!prefab) { Debug.LogError("Prefab cannot be null!"); return(null); } PoolBehaviour poolBhv = null; if (!s_instantiatePoolLookupDic.TryGetValue(prefab, out poolBhv) || !poolBhv) { bool isPrefab = !prefab.scene.IsValid(); if (isPrefab) { poolBhv = CreatePool(prefab); } } return(poolBhv); }
public static Object Instantiate(Object original, Transform parent = null, bool instantiateInWorldSpace = false) { if (!original) { Debug.LogError("original cannot be null!"); return(null); } if (original is Component || original is GameObject) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { Object obj = original is Component? poolBhv.Instantiate(original.GetType(), parent, instantiateInWorldSpace) as Object : poolBhv.Instantiate(parent, instantiateInWorldSpace) as Object ; return(obj); } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, parent, instantiateInWorldSpace)); #else return(Object.Instantiate(original)); #endif } } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, parent, instantiateInWorldSpace)); #else return(Object.Instantiate(original)); #endif } }
/// <summary> /// Instantiates a new instance using the prefab pool or creating a new pool if needed. /// </summary> /// <param name="original"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="parent"></param> /// <returns></returns> public static Object Instantiate(Object original, Vector3 position, Quaternion rotation = default(Quaternion), Transform parent = null) { if (!original) { Debug.LogError("original cannot be null!"); return(null); } if (original is Component || original is GameObject) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { Object obj = original is Component? poolBhv.Instantiate(original.GetType(), position, rotation, parent) as Object : poolBhv.Instantiate(position, rotation, parent) as Object ; return(obj); } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, position, rotation, parent)); #else return(Object.Instantiate(original, position, rotation)); #endif } } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, position, rotation, parent)); #else return(Object.Instantiate(original, position, rotation)); #endif } }
private void OnEnable() { m_target = target as PoolBehaviour; m_target.Initialize(); }
/// <summary> /// Registers an object in a pool. /// </summary> /// <param name="poolBhv"></param> /// <param name="obj"></param> public static void RegisterObjInPool(PoolBehaviour poolBhv, GameObject obj) { s_destroyPoolLookupDic[obj] = poolBhv; }