public GameObject SpawnFromPool(string tag, Vector2 position, Quaternion rotaion) { if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag " + tag + "doesn't exist"); return(null); } GameObject gameObjectToSpawn = poolDictionary[tag].Dequeue(); gameObjectToSpawn.SetActive(true); gameObjectToSpawn.transform.position = position; gameObjectToSpawn.transform.rotation = rotaion; IPoolObject poolObject = gameObjectToSpawn.GetComponent <IPoolObject>(); if (poolObject != null) { poolObject.OnObjectSpawn(); } poolDictionary[tag].Enqueue(gameObjectToSpawn); return(gameObjectToSpawn); }
/// <summary> /// Pushs back the item to the pool. /// </summary> /// <param name="obj">A reference to the item to be pushed back.</param> /// <param name="retainObject">If set to <c>true</c> retain object.</param> /// <param name="newParent">The Transform container to store the item.</param> public void PushToPool(ref GameObject obj, bool retainObject = true, Transform newParent = null) { if (obj == null) { return; } if (retainObject == false) { Object.Destroy(obj); obj = null; return; } if (newParent != null) { obj.transform.parent = newParent; } IPoolObject poolObject = obj.GetComponent <IPoolObject>(); if (poolObject != null) { GameObject prefab = poolObject.Prefab; Queue <GameObject> queue = FindInContainer(prefab); queue.Enqueue(obj); obj.SetActive(false); } obj = null; }
///<summary> ///Spawn PoolObject ///</summary> public GameObject Spawn(string _tag, Vector3 _spawnPos, Quaternion _spawnRot, Transform _parentTrans = null) { GameObject spawnObj = GetPoolObject(_tag, _parentTrans); if (spawnObj == null) { Debug.LogError("Not found GameObject : " + _tag); return(null); } spawnObj.SetActive(true); spawnObj.transform.position = _spawnPos; spawnObj.transform.rotation = _spawnRot; IPoolObject poolObj = spawnObj.GetComponent <IPoolObject>(); if (poolObj == null) { Debug.LogError("Not found IPoolObject : " + _tag); return(null); } poolObj.OnSpawnObject(); return(spawnObj); }
private void DespawnZombie(IPoolObject obj) { if (obj.PoolTransform.gameObject.activeInHierarchy) { _zombiePool.Recycle(obj); } }
private void SpawnZombie(int id, Transform poolTransform) { if (_alreadyEnergy.Contains(id)) { return; } _alreadyEnergy.Add(id); List <Transform> spawnPoints = FindAllChildrenRecursiveWithTag(poolTransform, Tag.ZombieSpawn); spawnPoints.Shuffle(); for (int i = 0; i < spawnPoints.Count && i < ZombieSpawnCount; i++) { Transform point = spawnPoints[i]; IPoolObject poolingObj = _zombiePool.Get(); poolingObj.PoolTransform.position = point.transform.position; // set random zombie sprite // need to refactor it SpriteRenderer zombieSpriteRenderer = poolingObj.PoolTransform.GetComponentInChildren <SpriteRenderer> (); int randomZombieNumber = Random.Range(0, ZombiePrefabs.Length); zombieSpriteRenderer.sprite = ZombiePrefabs [randomZombieNumber].GetComponentInChildren <SpriteRenderer> ().sprite; // finally activate poolingObj.PoolTransform.gameObject.SetActive(true); } }
/// <summary> /// Funzione che di Setup /// </summary> public void Setup() { poolDictionary = new Dictionary <ObjectTypes, List <IPoolObject> >(); foreach (PoolObjects obj in poolObjects) { List <IPoolObject> objectsToAdd = new List <IPoolObject>(); for (int i = 0; i < obj.ammount; i++) { GameObject instantiateObject = Instantiate(obj.prefab, transform); IPoolObject instantiateObjectInterface = instantiateObject.GetComponent <IPoolObject>(); if (instantiateObjectInterface != null) { instantiateObjectInterface.OnObjectDestroy += OnObjectDestroy; instantiateObjectInterface.OnObjectSpawn += OnObjectSpawn; OnObjectDestroy(instantiateObjectInterface); objectsToAdd.Add(instantiateObjectInterface); instantiateObjectInterface.PoolInit(); } else { Debug.Log("il prefab: " + instantiateObject.ToString() + " type:" + obj.objectType.ToString() + " non implementa l'interfaccia IPoolObject"); break; } } poolDictionary.Add(obj.objectType, objectsToAdd); } }
public IPoolObject <GameObject> Get() { IPoolObject <GameObject> poolObject = null; if (objectsInPools.Count == 0) { GameObject content = GameObject.Instantiate(prefab); poolObject = new GameObjectPoolObject(this, content); poolObject.Content.SetActive(true); objectsPoped.Add(poolObject); } else { int lastIndex = objectsInPools.Count - 1; poolObject = objectsInPools[lastIndex]; poolObject.Content.SetActive(true); objectsInPools.RemoveAt(lastIndex); objectsPoped.Add(poolObject); } poolObject.Content.transform.SetParent(null); return(poolObject); }
/// <summary> /// Funzione che spawna tutti gli oggetti da mettere in pool /// e controlla se implementano l'interaccia IPoolObject /// </summary> public void Init() { instance = this; poolDictionary = new Dictionary <ObjectTypes, List <IPoolObject> >(); poolParent = new GameObject("Pool").transform; poolParent.parent = transform; foreach (PoolObjects obj in poolObjects) { List <IPoolObject> objectsToAdd = new List <IPoolObject>(); Transform spawnParent = new GameObject(obj.objectType.ToString()).transform; spawnParent.parent = poolParent; for (int i = 0; i < obj.ammount; i++) { GameObject instantiateObject = Instantiate(obj.prefab, spawnParent); IPoolObject instantiateObjectInterface = instantiateObject.GetComponent <IPoolObject>(); if (instantiateObjectInterface != null) { instantiateObjectInterface.OnObjectDestroy += OnObjectDestroy; instantiateObjectInterface.OnObjectSpawn += OnObjectSpawn; instantiateObjectInterface.Setup(); OnObjectDestroy(instantiateObjectInterface); objectsToAdd.Add(instantiateObjectInterface); } else { Debug.Log("il prefab: " + instantiateObject.ToString() + " type:" + obj.objectType.ToString() + " non implementa l'interfaccia IPoolObject"); break; } } poolDictionary.Add(obj.objectType, objectsToAdd); } LevelManager.OnPlayerDeath += ResetPool; }
public void InitPool(uint _objectCount) { if (_objectCount == 0) { ClearPool(); } else { while (poolQueue.Count > _objectCount) { Destroy(poolQueue.Dequeue().GetGameObject()); } while (poolQueue.Count < _objectCount) { #region UNITY_EDITOR #if UNITY_EDITOR if (poolRoot == null) { poolRoot = new GameObject("" + objectPrefab.name + "Pool").transform; poolRoot.position = Vector3.zero; poolRoot.eulerAngles = Vector3.zero; } #endif #endregion IPoolObject instance = Instantiate(objectPrefab).GetComponent <IPoolObject>(); instance.InitObject(this); Recycle(instance); } } }
/// <summary> /// Recycle specified instance to pool. /// </summary> /// <param name="obj">Instance to recycle.</param> /// <summary> /// Recycle specified instance to pool. /// </summary> /// <param name="obj">Instance to recycle.</param> /// <param name="checkForDoubleRecycle">Check if instance already was recycled. Use false for performance boost.</param> public void Recycle(IPoolObject obj, bool checkForDoubleRecycle = true) { if ((object)obj != null) { #if UNITY_EDITOR if ((object)obj.PoolContainer != (object)this) { Debug.LogWarning("Invalid obj to recycle", (UnityEngine.Object)obj); return; } #endif var tr = obj.PoolTransform; if ((object)tr != null) { tr.gameObject.SetActive(false); if ((object)tr.parent != (object)_itemsRoot) { tr.SetParent(_itemsRoot, true); } } if (checkForDoubleRecycle && _store.Contains(obj)) { #if UNITY_EDITOR Debug.LogWarning("Object already was recycled", (UnityEngine.Object)obj); #endif } else { _store.Push(obj); } } }
static IPoolObject GetImpl(Type objType) { IPoolObject poolObject = null; List <IPoolObject> poolObjectList = null; if (!_poolObjectDict.TryGetValue(objType, out poolObjectList)) { poolObjectList = new List <IPoolObject>(); _poolObjectDict.Add(objType, poolObjectList); } else { poolObject = poolObjectList.Where(obj => !obj.IsInUse).FirstOrDefault(); if (poolObject != null) { poolObject.IsInUse = true; } } if (poolObject == null) { poolObject = Activator.CreateInstance(objType) as IPoolObject; poolObject.IsInUse = true; poolObjectList.Add(poolObject); } return(poolObject); }
public GameObject GetObject(Vector3 position, Quaternion rotation) { IPoolObject pobj = null; foreach (var obj in _objects) { if (obj.IsAvailable) { pobj = obj; break; } } if (pobj == null) { pobj = Object.Instantiate(Prefab, GetGlobalParent()).GetComponent <IPoolObject>(); pobj.OnInstantiated(); _objects.Add(pobj); } GameObject go = (pobj as Component).gameObject; go.transform.position = position; go.transform.rotation = rotation; pobj.OnEnabled(); return(pobj.GameObject); }
public GameObject SpawnFromPool(string name, Vector3 position = new Vector3(), Quaternion rotation = new Quaternion()) { if (!poolDic.ContainsKey(name)) { Debug.LogWarning("No " + name + "in the Pooling Dictionary!"); } GameObject objToSpawn = poolDic[name].Dequeue(); //Set for the basis objToSpawn.transform.position = position; objToSpawn.transform.rotation = rotation; //Implement the Start method IPoolObject poolObject = objToSpawn.GetComponent <IPoolObject>(); if (poolObject != null) { poolObject.PoolObjectStart(); } poolDic[name].Enqueue(objToSpawn); objToSpawn.SetActive(true); return(objToSpawn); }
public GameObject PoolSpawn(string newTag, Vector3 position, Quaternion rotation) { if (!poolDictionary.ContainsKey(newTag)) { Debug.LogWarning(newTag + " does not exist."); return(null); } for (int i = 0; i < poolDictionary.Count; i++) { Debug.LogWarning(poolDictionary.Values); } GameObject newObjSpawn = poolDictionary[newTag].Dequeue(); newObjSpawn.SetActive(true); newObjSpawn.transform.position = position; newObjSpawn.transform.rotation = rotation; IPoolObject newPoolObj = newObjSpawn.GetComponent <IPoolObject>(); if (newPoolObj != null) { newPoolObj.onObjectSpawn(); } poolDictionary[newTag].Enqueue(newObjSpawn); return(newObjSpawn); }
/// <summary> /// Funzione che gestisce l'evento di distruzione di un oggetto per farlo tornare i Pool /// </summary> /// <param name="objectToDestroy"></param> private void OnObjectDestroy(IPoolObject objectToDestroy) { objectToDestroy.currentState = State.InPool; objectToDestroy.gameObject.transform.position = poolPosition; objectToDestroy.gameObject.transform.SetParent(transform); objectToDestroy.ownerObject = null; }
private void DespawnCoin(IPoolObject obj) { if (obj.PoolTransform.gameObject.activeInHierarchy) { _coinsPool.Recycle(obj); } }
public IPoolObject Get(string id) { if (!_poolObjects.ContainsKey(id)) { return(null); } var pool = _poolObjects[id]; if (pool.Count == 0) { if (!OnPoolEmpty.Invoke(id)) { return(null); } } IPoolObject obj = pool.Dequeue(); obj.OnActivated(); ActiveObjects.Add(obj); return(obj); }
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation) { if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag" + tag + "doesn't excist."); return(null); } // 从队列中获取obj GameObject objectToSpawn = poolDictionary[tag].Dequeue(); objectToSpawn.SetActive(true); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; IPoolObject pooledObj = objectToSpawn.GetComponent <IPoolObject>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn); }
public virtual void Clear() { id = 0; campflag = 0; name = ""; type = 0; config = ""; //表的ID scale = 1; position = Vector3.zero; rotation = Quaternion.identity; machine.Clear(); model = null; ResetProperty(); this.Foreach((component) => { IPoolObject poolObject = component as IPoolObject; if (poolObject != null && this != component) { ObjectPool.ReturnInstance(poolObject); } }); this.RemoveAllComponents(); }
IPoolObject Get(Type poolType, Type objType) { IPoolObject poolObject = null; List <IPoolObject> poolObjectList = null; if (!_poolObjectDict.TryGetValue(poolType, out poolObjectList)) { poolObjectList = new List <IPoolObject>(); _poolObjectDict.Add(poolType, poolObjectList); } else { for (var i = 0; i < poolObjectList.Count; i++) { var tmp = poolObjectList[i]; if (!tmp.IsInUse && tmp.GetType() == objType) { tmp.IsInUse = true; poolObject = tmp; break; } } } if (poolObject == null) { poolObject = Activator.CreateInstance(objType) as IPoolObject; poolObject.IsInUse = true; poolObjectList.Add(poolObject); } return(poolObject); }
public void Release(IPoolObject obj) { obj.OnReleased(); OnReleased?.Invoke(obj); // remove from active object and send back to pool ActiveObjects.Remove(obj); _poolObjects[obj.ObjId].Enqueue(obj); }
public void Push(IPoolObject val) { if (val == null) { return; } val.InUse = false; }
public static IPoolObject Get(Type objType, IPoolObjectOwner owner, IEventArgs arg) { IPoolObject o = Instance[objType].Get(arg); SetInfo(o, owner, objType); //Log.W(type.ToString() + "Get Idel" + Instance.dic[type].CacheCount + " worker " + Instance.dic[type].WorkerCount); return(o); }
private void AddToCache(IPoolObject val) { if (!_cache.ContainsKey(val.GetType())) { _cache[val.GetType()] = new List <IPoolObject>(); } _cache[val.GetType()].Add(val); }
protected void Spawn(Vector3 pos, Quaternion rot) { IPoolObject enemy = GameMan.Instance.ObjPoolMan.GetObjectFromPool(objectPoolType); enemy.Activate(pos, rot); spawnAmount--; currentTime = 0f; }
public void Add(IPoolObject obj) { obj.InitPoolObject(this); if (!_poolObjects.ContainsKey(obj.ObjId)) { _poolObjects[obj.ObjId] = new Queue <IPoolObject>(); } _poolObjects[obj.ObjId].Enqueue(obj); }
public override void Unspawn(IPoolObject obj) { if (null == obj) { return; } obj.OnUnspawn(); mQueue.Enqueue((T)obj); }
private void DestroyObject(GameObject obj) { // Destroy the object collected IPoolObject poolObj = obj.GetComponent <IPoolObject>(); if (poolObj != null) { Pool.RemoveObject(poolObj.GetPoolName(), obj); } }
public void CreateObjects() { // Instantiate objects for wanted amount, place them in the pool and initialise them. for (int i = 0; i < poolStartSize; i++) { IPoolObject newObj = Instantiate(prefab); pool.Enqueue((T)newObj); newObj.ReadyUp(this); } }
public static void Release <T>(IPoolObject iobj) where T : class, IPoolObject { T t = iobj as T; if (t != null) { TsLog.LogWarning("Release T", new object[0]); ObjectPoolManager.Release <T>(t); } }
/// <summary> /// Recycle specified instance to pool. /// </summary> /// <param name="obj">Instance to recycle.</param> public void Recycle(IPoolObject obj) { if ((object) obj != null) { #if UNITY_EDITOR if (obj.PoolContainer != this) { Debug.LogWarning ("Invalid obj to recycle", (Object) obj); return; } #endif var tr = obj.PoolTransform; if ((object) tr != null) { tr.gameObject.SetActive (false); } if (!_store.Contains (obj)) { _store.Push (obj); } } }