/// <summary> /// Gets a new object for the name type provided. If no object type /// exists or if onlypooled is true and there is no objects of that /// type in the pool /// then null will be returned. /// </summary> /// <returns> /// The object for type. /// </returns> /// <param name='objectType'> /// Object type. /// </param> /// <param name='onlyPooled'> /// If true, it will only return an object if there is one currently /// pooled. /// </param> public GameObject GetObjectForType(PooledObjectType objectType, bool onlyPooled) { int idx = pooledTypeToIdx(objectType); if (idx < 0 || idx >= objectPrefabs.Length) { return null; } if (pooledObjects[idx].Count > 0) { GameObject pooledObject = pooledObjects[idx][0]; pooledObjects[idx].RemoveAt(0); pooledObject.transform.parent = null; pooledObject.SetActiveRecursively(true); return pooledObject; } else if (! onlyPooled) { GameObject prefab = objectPrefabs[idx]; GameObject newObject = Instantiate(prefab) as GameObject; newObject.name = prefab.name; return newObject; } //If we have gotten here either there was no object of the specified //type or none were left in the pool with onlyPooled set to true return null; }
private GameObject ExpandPool(PooledObjectType pooledObjectType, Vector3 pos, Quaternion rot) { int index = _poolIndexes[pooledObjectType]; GameObject objToAdd = Instantiate(_pool[index].Prefab, _poolMasters[pooledObjectType], true); objToAdd.SetActive(true); objToAdd.transform.position = pos; objToAdd.transform.rotation = rot; IPooledObject iPool = objToAdd.GetComponent <IPooledObject>(); if (iPool == null) { PooledObject tempPool = objToAdd.AddComponent <PooledObject>(); iPool = tempPool; } iPool.PoolType = pooledObjectType; IPooledObject iPooledObj = objToAdd.GetComponent <IPooledObject>(); IObjectPoolInitializable iInitializable = objToAdd.GetComponent <IObjectPoolInitializable>(); iInitializable?.Init(); iPooledObj.OnObjectSpawn(); _poolDictionary[pooledObjectType].Enqueue(objToAdd); _poolDictionary[pooledObjectType].Dequeue(); _pool[index].Size++; return(objToAdd); }
private GameObject ExpandPool(PooledObjectType tag, Vector3 pos, Quaternion rot) { int index = _poolIndexes[tag]; GameObject temp = Instantiate(Pool[index].Prefab); temp.SetActive(true); temp.transform.SetParent(_poolMasters[tag]); temp.transform.position = pos; temp.transform.rotation = rot; if (temp.GetComponent <IPooledObject>() == null) { PooledObject tempPool = temp.AddComponent <PooledObject>(); tempPool.Type = tag; } IPooledObject iPooledObj = temp.GetComponent <IPooledObject>(); iPooledObj.Init(); iPooledObj.OnObjectSpawn(); PoolDictionary[tag].Enqueue(temp); Pool[index].Size++; return(temp); }
public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion rot) { if (!PoolDictionary.ContainsKey(tag)) { Debug.LogWarning("PoolObjects with Tag " + tag + " doesn't exist .."); return(null); } GameObject objToSpawn; if (PoolDictionary[tag].Count != 0) { objToSpawn = PoolDictionary[tag].Peek(); objToSpawn.SetActive(true); objToSpawn.transform.position = pos; objToSpawn.transform.rotation = rot; IPooledObject iPooledObj = objToSpawn.GetComponent <IPooledObject>(); iPooledObj.Init(); iPooledObj.OnObjectSpawn(); PoolDictionary[tag].Dequeue(); } else { objToSpawn = ExpandPool(tag, pos, rot); } return(objToSpawn); }
public void Despawn(PooledObjectType tag, GameObject objToDespawn) { PoolDictionary[tag.ToString()].Enqueue(objToDespawn); objToDespawn.SetActive(false); objToDespawn.transform.position = Vector3.zero; IPooledObject iPooledObj = objToDespawn.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectDespawn(); } }
public void Despawn(PooledObjectType tag, GameObject obj) { PoolDictionary[tag].Enqueue(obj); IPooledObject iPooledObj = obj.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectDespawn(); } obj.SetActive(false); }
public AllocatedPrefab(Transform pool, IPooledObject prefab) { Debug.Assert(!(prefab.AmountInPool == 0 && prefab.Type == PooledObjectType.NotImportant), "Start amount of not important is 0: " + prefab.ThisObject.name); this.Prefab = prefab; this.type = prefab.Type; this.pool = pool; this.allocated = new List <IPooledObject>(); this.prevReturnedIndex = -1; for (int i = 0; i < prefab.AmountInPool; i++) { AllocateNew(false, false); } }
public void Despawn(GameObject obj) { PooledObjectType tag = obj.GetComponent <IPooledObject>().PoolType; if (tag != null && PoolDictionary.ContainsKey(tag)) { PoolDictionary[tag].Enqueue(obj); IPooledObject iPooledObj = obj.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectDespawn(); } obj.SetActive(false); } else { Debug.LogError("Trying to despawn object which is not pooled !"); } }
public void Despawn(GameObject obj) { PooledObjectType pooledObjectType = obj.GetComponent <IPooledObject>().PoolType; if (_poolDictionary.ContainsKey(pooledObjectType) && // check if there's a queued objects by that tag. _poolDictionary[pooledObjectType].Contains(gameObject)) // check if `obj` is already despawned { _poolDictionary[pooledObjectType].Enqueue(obj); IPooledObject iPooledObj = obj.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectDespawn(); } obj.transform.SetParent(_poolMasters[pooledObjectType]); obj.SetActive(false); } else { Debug.LogError("Trying to despawn object which is not pooled or object is already despawned !"); } }
public GameObject SpawnFromPool(PooledObjectType pooledObjectType, Vector3 pos, Quaternion rot, GameObject parent = null) { if (!_poolDictionary.ContainsKey(pooledObjectType)) { Debug.LogWarning("PoolObjects with Tag " + pooledObjectType + " doesn't exist .."); return(null); } GameObject objToSpawn; if (_poolDictionary[pooledObjectType].Count != 0) { objToSpawn = _poolDictionary[pooledObjectType].Peek(); objToSpawn.SetActive(true); objToSpawn.transform.position = pos; objToSpawn.transform.rotation = rot; IPooledObject iPooledObj = objToSpawn.GetComponent <IPooledObject>(); IObjectPoolInitializable iInitializable = objToSpawn.GetComponent <IObjectPoolInitializable>(); iInitializable?.Init(); // iPooledObj.Init(iPooledObj.ObjectPooler); iPooledObj.OnObjectSpawn(); _poolDictionary[pooledObjectType].Dequeue(); } else { objToSpawn = ExpandPool(pooledObjectType, pos, rot); } if (parent) { objToSpawn.transform.SetParent(parent.transform); } return(objToSpawn); }
public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion rot) { if (!PoolDictionary.ContainsKey(tag.ToString())) { Debug.LogWarning("PoolObjects with Tag " + tag + " doesn't exist .."); return(null); } GameObject objToSpawn = PoolDictionary[tag.ToString()].Dequeue(); objToSpawn.SetActive(true); objToSpawn.transform.position = pos; objToSpawn.transform.rotation = rot; IPooledObject iPooledObj = objToSpawn.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectSpawn(); } PoolDictionary[tag.ToString()].Enqueue(objToSpawn); return(objToSpawn); }
private int pooledTypeToIdx(PooledObjectType objectType) { int ret = -1; switch (objectType) { case PooledObjectType.RedHood: ret = 0; break; case PooledObjectType.Blood: ret = 1; break; case PooledObjectType.Granny: ret = 2; break; case PooledObjectType.Hunter: ret = 3; break; case PooledObjectType.Rabbit: ret = 4; break; default: break; } return ret; }
/// <summary> /// Pools the object specified. Will not be pooled if there are no prefab of that type. /// </summary> /// <param name='obj'> /// Object to be pooled. /// </param> public bool PoolObject(PooledObjectType objectType, GameObject obj) { int idx = pooledTypeToIdx(objectType); if (idx < 0 || idx >= objectPrefabs.Length) { return false; } obj.SetActiveRecursively(false); obj.transform.parent = containerObject.transform; pooledObjects[idx].Add(obj); return true; }