/// <summary> /// Get the pool for the given prefab's type, creating one if it does not yet exist /// </summary> /// <param name="prefab"> /// Prefab to get pool for /// </param> /// <returns> /// Pool for given type /// </returns> public static MonoBehaviourPool GetPool(PooledMonoBehaviour prefab) { GameObject obj; MonoBehaviourPool pool; if (Application.isEditor) { obj = GameObject.Find(prefab.name + " Pool"); if (obj != null) { pool = obj.GetComponent <MonoBehaviourPool>(); if (pool != null) { return(pool); } } } obj = new GameObject(prefab.name + " Pool"); DontDestroyOnLoad(obj); pool = obj.AddComponent <MonoBehaviourPool>(); pool._pooledPrefab = prefab; pool._prefabString = prefab.name; pool.transform.SetParent(GameObject.FindGameObjectWithTag("Pools").transform); return(pool); }
/// <summary> /// Add the given object to the pool /// </summary> /// <param name="obj"> /// Object to add to the pool /// </param> public void AddObject(PooledMonoBehaviour obj) { obj.Initialize(); obj.gameObject.SetActive(false); obj.transform.SetParent(transform, false); obj.name = "Pooled " + _prefabString; _pooledObjects.Add(obj); }
/// <summary> /// Set the prefab to pool /// </summary> /// <param name="prefab"> /// Prefab to pool /// </param> public void SetPrefab(PooledMonoBehaviour prefab) { _pooledPrefab = prefab; }