/// <summary> /// Headstarts a pool and grows it. /// </summary> /// <param name="Size">Number of objects to instantiate</param> public void InstantiatePool(int Size) { //Debug.Log("Instantiating pool for , " + ObjectName + "," + Size + "Times"); //What if there is no original object here if (Original == null) { Debug.LogError("Cannot Instantiate Pool, Original is empty"); } for (int i = 0; i < Size; i++) { //make a new Object From Original GameObject ToAdd = Instantiate(Original); //deactivate object for storage ToAdd.SetActive(false); //Add PoolMember Script PoolMember pm = ToAdd.AddComponent <PoolMember>(); pm.MyPool = this; //Trigger Event pm.OnPoolEnter(); //Add to Pool Objects.Add(ToAdd); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { var isParentAttachment = false; PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { //not on main object, so check parent //hack by Dale pm = obj.GetComponentInParent <PoolMember>(); isParentAttachment = true; } if (pm == null) { if (obj.activeInHierarchy) { //Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); } GameObject.Destroy(obj); } else { if (isParentAttachment) { pm.myPool.Despawn(obj.transform.parent.gameObject); } else { pm.myPool.Despawn(obj); } } }
/// <summary> /// Adds An object to pool /// </summary> /// <param name="ToGive">Object to add to the pool. ATTENTION! it must be like original, or else...</param> public void AddNew(GameObject ToGive) { //Deactivate object for storage ToGive.SetActive(false); //Add PoolMember Script if there is NOTT PoolMember pm = ToGive.GetComponent <PoolMember>(); if (pm == null) { pm = ToGive.AddComponent <PoolMember>(); //and if we fail to add PoolMember to object. <EPIC-FAIL> if (pm == null) { Debug.LogError("There is a problem on Pool.AddNew, We cant add PoolMember to it. ''Sorry man. I just cant...''"); } } //set pool for new PoolMember pm.MyPool = this; //Trigger On Pool Enter. We want it to be deactivated and ready to be pooled pm.OnPoolEnter(); //And Add to pool as usual Objects.Add(ToGive); }
private static ObjectPool GetObjectPool(GameObject obj, PoolMember member) { if (member == null) { return(GetObjectPool(obj, true)); } return(member.pool); }
// Despawn the given object back into its pool. static public void Despawn(GameObject instance) { // Get right pool PoolMember pm = instance.GetComponent <PoolMember> (); if (pm == null) { Debug.Log("Object " + instance.name + " was not spawned from a pool. Destroying it instead."); } pm.myPool.Despawn(instance); }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { GameObject.Destroy(obj); } else { pm.myPool.Despawn(obj); } }
public static void Despawn(GameObject prefab) { PoolMember pmTemp = prefab.GetComponent <PoolMember>(); if (pmTemp == null) { GameObject.Destroy(prefab); } else { pmTemp.myPool.Despawn(prefab); } }
private void Despawn() { PoolMember pm = this.GetComponent <PoolMember>(); if (pm != null) { pm.Despawn(gameObject); } else { Destroy(this.gameObject); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> public static void Despawn(GameObject obj, bool deparent = false) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Object.Destroy(obj); } else { pm.myPool.Despawn(obj, deparent); } }
/// <summary> /// Despawn the specified prefab. /// </summary> public static void Destroy(GameObject entity, float time = 0) { PoolMember pm = entity.GetComponent <PoolMember> (); if (null == pm) { Debug.LogError("GameObject '" + entity.name + "' wasn't spawned from a pool."); } else { pm.Destroy(time); } }
static public void Despawn(GameObject gameObject) { PoolMember poolMember = gameObject.GetComponent <PoolMember>(); if (poolMember == null) { Debug.Log("Objects '" + gameObject.name + "' wasn't spawned from a pool. Destroying it instead."); NetworkServer.Destroy(gameObject); } else { poolMember.myPool.Despawn(gameObject); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); GameObject.Destroy(obj); } else { pm.myPool.Despawn(obj); } }
static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' pool'dan gelmemis kardes. Destroy amk!"); GameObject.Destroy(obj); } else { pm.myPool.Despawn(obj); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Destroy(obj); // if the object does not belong to any pool then we should delete it } else { pm.myPool.PushPickup(obj); Invoke("Spawn", 5.0f); } }
// Despawn the specified object back into its pool. public static void Despawn <T>(T clone) where T : MonoBehaviour { PoolMember pm = clone.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + clone.name + "' wasn't spawned from a pool. Destroying it instead."); GameObject.Destroy(clone); } else { pm.myPool.Despawn(clone); } }
///Summary ///Checks that the object is a poolmember and then Despawns it, if it is not a poolmember it destroys it. ///Summary static public void Despawn(GameObject objToDespawn) { PoolMember poolMember = objToDespawn.GetComponent <PoolMember>(); if (poolMember == null) { Debug.Log("Object " + objToDespawn.name + " was not spawned from a pool. Destroying it."); GameObject.Destroy(objToDespawn); } else { poolMember.pool.Despawn(objToDespawn); } }
static public void despawn(GameObject go) { PoolMember pm = go.GetComponent <PoolMember> (); if (pm == null) { //Debug.Log ("Object '" + go.name + "' wasn't spawned from a pool. Destroying it instead"); GameObject.Destroy(go); } else { pm.myPool.despawn(go); } }
static public void Despawn(GameObject currentObject) { if (currentObject != null) { PoolMember poolMember = currentObject.GetComponent <PoolMember>(); if (poolMember == null) { Object.Destroy(currentObject); } else { poolMember.pool.Despawn(currentObject); } } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> public void Despawn <T>(T obj) where T : Behaviour { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); GameObject.Destroy(obj); } else { Pool <T> pool = pm.myPool as Pool <T>; pool.Despawn(obj); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); GameObject.Destroy(obj); } else { //Debug.Log("This is causing you error - no longer, maybe."); pm.myPool.Despawn(obj); } }
/// <summary> /// Spawns the prefabs then deactivates them ready to be used. /// <para></para> /// <b>NOTE</b>: This will call Awake() and Start() on the spawned objects /// but it DOES NOT call <see cref="IPoolable.OnPoolSpawn"/> nor /// <see cref="IPoolable.OnPoolUnSpawn"/>. /// </summary> /// <param name="prefab">Prefab.</param> /// <param name="qty">Quantity.</param> static public void Preload(GameObject prefab, int qty = 1) { BeginDictionaryOrNewPool(prefab, qty); // Make an array to grab the objects we're about to pre-spawn. GameObject[] obs = new GameObject[qty]; for (int i = 0; i < qty; i++) { obs[i] = pools[prefab].PopFromPool(Vector3.zero, Quaternion.identity); } // Now despawn them all. for (int i = 0; i < qty; i++) { PoolMember member = obs[i].GetComponent <PoolMember>(); member.myPool.PushToPool(obs[i]); // Push without calling the recycle event } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> // Despawns the object and puts it back into its pool. /// </summary> /// <param name="obj"></param> public static void Despawn(GameObject obj) { PoolMember poolMember = obj.GetComponent <PoolMember>(); // Despawn the object through its attached pool if (poolMember != null) { poolMember.LinkedPool.Despawn(obj); } // The object wasn't spawned via an object pool, so just destroy it normally else { GameObject.Destroy(obj, 1f); } }
/// <summary> /// Collects the object to its pool then deactivates it, ready for next use. /// If it doesn't have a pool, it will be destroyed. /// This will call <see cref="IPoolable.OnPoolUnSpawn"/> /// </summary> static public void SendToPool(GameObject obj) { // ++ Instead of getComponent compare to an array PoolMember member = obj.GetComponent <PoolMember>(); if (member) { member.OnRecycleToPool(); member.myPool.PushToPool(obj); } else { Debug.LogWarning("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); Object.Destroy(obj); } }
static public bool Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool."); return(false); //GameObject.Destroy(obj); } else { pm.myPool.Despawn(obj); return(true); } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); // Take out this if statement to set the value using setter when ever you change it in the inspector. // But then it gets called a couple of times when ever inspector updates // By having a button, you can control when the value goes through the setter and getter, your self. if (GUILayout.Button("Return Pool")) { if (target.GetType() == typeof(PoolMember)) { PoolMember pm = (PoolMember)target; pm.ReturnPool(); Debug.Log("Returned"); } } }
static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { GameObject.Destroy(obj); } else { foreach (IDespawnable despawnScript in obj.GetComponentsInChildren <IDespawnable>()) { despawnScript.Despawn(); } pm.myPool.Despawn(obj); } }
/** * Creates an object */ GameObject CreateObject() { GameObject newClone = GameObject.Instantiate(sourceObject) as GameObject; //add object to pool and set member PoolMember member = newClone.AddComponent <PoolMember>(); member.pool = this; //If server, register the spawn if (isServer) { NetworkServer.Spawn(newClone); } //returns gameObject to be used to register client spawn handler in poolmemember return(newClone); }
/// <summary> /// Retorna una instancia a la piscina, usar en vez de Destroy. /// </summary> public void Despawn(GameObject instance) { PoolMember poolMember = instance.GetComponent <PoolMember>(); if (poolMember == null) { Debug.Log($"El objeto '{instance.name}' no fue instanciado desde una piscina. Se ha destruido."); Destroy(instance); } else { poolMember.MyPool.Despawn(instance); if (_poolStore == null) { _poolStore = new GameObject("(Pool) Store"); } instance.transform.parent = _poolStore.transform; } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.LogWarningFormat(obj, "Object '{0}' wasn't spawned from a pool. Destroying it instead.", obj.name); GameObject.Destroy(obj); } else { foreach (IDespawnable despawnScript in obj.GetComponentsInChildren <IDespawnable>()) { despawnScript.Despawn(); } pm.myPool.Despawn(obj); } }
/// <summary> /// Despawn the specified gameobject back into its pool. /// </summary> static public void Despawn(GameObject obj) { PoolMember pm = obj.GetComponent <PoolMember>(); if (pm == null) { Debug.Log("Object '" + obj.name + "' wasn't spawned from a pool. Destroying it instead."); GameObject.Destroy(obj); } else { try { pm.myPool.Despawn(obj); } catch (Exception ex) { Debug.Log("SimplePool Error: " + ex.Message + " " + ex.InnerException); } } }