private void ReturnObject(IPooledObject pooledObj) { pooledObj.DeactivateToPool(); if (objectPoolContainer != null) { pooledObj.GetGameObject().transform.parent = objectPoolContainer; } pooledObjects.Enqueue(pooledObj); }
/// <summary>Spawn an object into a specific position, parent and set if it's in world space or not /// <para>The CreatePools function must have been called before.</para> /// </summary> public static GameObject SpawnFromPool(string tag, Vector3 position, Transform parent, bool instantiateInWorldSpace) { if (!poolDictionary.ContainsKey(tag)) { Debug.Log("Pool with tag " + tag + " doesn't exist."); return(null); } if (!instantiateInWorldSpace) { SpawnFromPool(tag, position, parent); } Pool.PoolPrefab pool = poolDictionary[tag]; GameObject objectToSpawn; if (!pool.undetermined) { objectToSpawn = pool.determinedPool.Dequeue(); objectToSpawn.transform.localPosition = position; objectToSpawn.transform.localRotation = Quaternion.identity; objectToSpawn.transform.SetParent(parent); objectToSpawn.SetActive(true); pool.determinedPool.Enqueue(objectToSpawn); } else { if (pool.undeterminedPool.Count != 0) { int lastIndex = pool.undeterminedPool.Count - 1; objectToSpawn = pool.undeterminedPool[lastIndex]; objectToSpawn.transform.localPosition = position; objectToSpawn.transform.localRotation = Quaternion.identity; objectToSpawn.transform.SetParent(parent); objectToSpawn.SetActive(true); } else { objectToSpawn = Object.Instantiate(pool.prefab); objectToSpawn.transform.localPosition = position; objectToSpawn.transform.localRotation = Quaternion.identity; objectToSpawn.transform.SetParent(parent); objectToSpawn.AddComponent <PoolChecker>().poolTag = tag; } } IPooledObject pooledObj = objectToSpawn.GetComponent <IPooledObject>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } return(objectToSpawn); }
void checkIPooledObjectInterface(GameObject obj) { IPooledObject pooledObject = obj.GetComponent <IPooledObject>(); if (pooledObject != null) { pooledObject.OnObjectSpawn(); } }
public void ResetPoolObj(GameObject obj) { IPooledObject pooledObj = obj.GetComponent <IPooledObject>(); if (pooledObj != null) { pooledObj.OnObjectDespawn(); } }
private void ResetGameObject(GameObject go) { go.SetActive(true); IPooledObject goSpawned = go.GetComponent <IPooledObject>(); if (goSpawned != null) { goSpawned.OnSpawn(); } }
public bool Evict(EvictionConfig config, IPooledObject <T> underTest, int idleCount) { if ((config.IdleSoftEvictTime < underTest.IdleTimeMillis && config.MinIdle < idleCount) || config.IdleEvictTime < underTest.IdleTimeMillis) { return(true); } return(false); }
public PoolSet(IPooledObject originalPrefab) { if (originalPrefab == null) { throw new System.ArgumentNullException(nameof(originalPrefab)); } OriginalScript = originalPrefab; OriginalPrefab = originalPrefab.gameObject; }
public GameObject spawnFromPool(string tag, Vector3 position, Quaternion rotation) { float fallSpeed = Random.Range(3, 7); if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag " + tag + "doesn't excist."); return(null); } GameObject objectToSpawn = poolDictionary[tag].Dequeue(); objectToSpawn.SetActive(true); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; objectToSpawn.GetComponent <Rigidbody>().drag = fallSpeed; IPooledObject pooledObj = objectToSpawn.GetComponent <IPooledObject>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } //Main Branch를 list에 삽입. if (objectToSpawn.name == "MainBranch(Clone)") { Branch.Instance.mainBranchList.Add(objectToSpawn.transform.GetChild(0).gameObject.transform); Branch.Instance.mainBranchLeftList.Add(objectToSpawn.transform.GetChild(1).gameObject.transform); Branch.Instance.mainBranchRightList.Add(objectToSpawn.transform.GetChild(2).gameObject.transform); } //other Branch 들의 첫번째 자식 위치포지션을 list에 삽입. if (objectToSpawn.name == "ForsyBranch(Clone)") { Branch.Instance.mainBranchPosList.Add(objectToSpawn.transform.GetChild(0).gameObject.transform); } if (objectToSpawn.name == "LeafBranch(Clone)") { Branch.Instance.mainBranchPosList.Add(objectToSpawn.transform.GetChild(0).gameObject.transform); } if (objectToSpawn.name == "NormalBranch(Clone)") { Branch.Instance.mainBranchPosList.Add(objectToSpawn.transform.GetChild(0).gameObject.transform); } if (objectToSpawn.name == "BreakBranch(Clone)") { Branch.Instance.mainBranchPosList.Add(objectToSpawn.transform.GetChild(0).gameObject.transform); } poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn); }
/// <summary> /// 对象入队 /// </summary> /// <param name="pooledObject"></param> /// <returns></returns> private bool Enqueue(IPooledObject pooledObject) { if (_pooledObjects.Count == _maximumPoolSize) { return(false); } _pooledObjects.Enqueue(pooledObject); _poolSize++; return(true); }
/// <summary> /// 将对象放入对象池 /// </summary> /// <param name="pooledObject"></param> public void Release(IPooledObject pooledObject) { if (Enqueue(pooledObject)) { pooledObject.OnPoolReset(); } else { DestroyPooledObject(pooledObject); } }
void CreateNewPooledObject(int numberOfObjects) { numberOfObjects = Mathf.Clamp(numberOfObjects, 0, maxPoolCapacity); for (int i = 0; i < numberOfObjects; i++) { IPooledObject temp = Instantiate(prefab).GetComponent <IPooledObject>(); temp.Init(this); temp.Destroy(); freed.Enqueue(temp); } }
public bool ValidateObject(IPooledObject <SampleClass> @object) { if (DateTime.Now - @object.CreateTime > TimeSpan.FromSeconds(5)) { return(@object.Object.Id % 2 == 0); } else { return(true); } }
private Transform Create(Queue <Transform> queue, PooledObjectStats stats) { Transform pooledObject = _resourceFactory.Instantiate(stats.prefab, new Vector3(0, YPOS, 0), stats.parent); IPooledObject pooledObjectComponent = pooledObject.GetComponent <IPooledObject>(); if (pooledObjectComponent != null) { pooledObjectComponent.PooledObjectStats = stats; } pooledObject.gameObject.SetActive(false); return(pooledObject); }
/// <summary> /// Create an object, and place it into the pool. addObject() is useful for /// "pre-loading" a pool with idle objects. /// </summary> public void AddObject() { AssertOpen(); if (factory == null) { throw new IllegalStateException( "Cannot add objects without a factory."); } IPooledObject <T> p = Create(); AddIdleObject(p); }
// ================================================================================ // private methods // -------------------------------------------------------------------------------- private IPooledObject CreateNewObject(Vector3?pos = null) { GameObject newObject = GameObjectFactory.GameObject(_prefab, position: pos); IPooledObject instance = newObject.transform.GetInterface <IPooledObject>(); if (instance == null) { Debug.LogError("Object " + _prefab + " has no Type " + typeof(IPooledObject)); } return(instance); }
public static void DestroyFromPool(IPooledObject script) { // Deactivate the script first ReturnToPool(script); // Check if the script isn't null if (script != null) { // Destroy the game object Destroy(script.gameObject); } }
void CheckNext() { if (!fetchedNext) { nextAvailable = idleObjectIterator.MoveNext(); if (nextAvailable) { next = idleObjectIterator.Current; } fetchedNext = true; } }
public IPooledObject GetConnection() { lock (objLock) { IPooledObject pooledObject = pooledObjects.FirstOrDefault(); if (pooledObject != null) { pooledObjects.Remove(pooledObject); } return(pooledObject); } }
public void Despawn(PooledObjectType tag, GameObject obj) { PoolDictionary[tag].Enqueue(obj); IPooledObject iPooledObj = obj.GetComponent <IPooledObject>(); if (iPooledObj != null) { iPooledObj.OnObjectDespawn(); } obj.SetActive(false); }
/// <summary> /// 释放对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pooledObject"></param> public void ReleaseObject(IPooledObject pooledObject) { Type type = pooledObject.GetType(); IObjectPool objectPool = GetObjectPool(type); if (objectPool == null) { objectPool = RegisterObjectPool(type); } objectPool.Release(pooledObject); }
public void PutBackInPool(IPooledObject pooledObject) { if (pooledObject == null) { return; } if (activeObjects.Contains(pooledObject)) { activeObjects.Remove(pooledObject); freed.Enqueue(pooledObject); } }
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(); } }
private void ReInitialize(Transform spawnedObject) { IPooledObject pooledObjectComponent = spawnedObject.GetComponent <IPooledObject>(); if (pooledObjectComponent != null) { spawnedObject.GetComponent <IPooledObject>().ReInitialize(); } else { Debug.LogWarning("Pooled object class should inherits IPooledObject to use ReInitialize usage"); } }
// return an Object to the pool public void Push(IPooledObject item) { item.ToggleOff(); //if (item is MonoBehaviour) // (item as MonoBehaviour).transform.position = new Vector3(-1000.0f, -1000.0f, -1000.0f); item.getsDisabled -= ObjectGetsDisabled; if (_activeObjects.Contains(item)) _activeObjects.Remove(item); _pool.Push(item); }
void CreateGarbage(Vector3 pos, Vector3Int tilePosition) { GameObject newGarbage = ObjectPoolerUpgraded.Instance.SpawnFromPool("Garbage", pos, Quaternion.identity); //Garbage newGarbage = Instantiate(garbagePrefab); newGarbage.transform.position = map.GetCellCenterWorld(tilePosition); IPooledObject pooledObject = newGarbage.GetComponent <IPooledObject>(); pooledObject.OnObjectSpawn(tilePosition, this); //newGarbage.SetOnMap(tilePosition, this); garbageOnMap.Add(tilePosition); }
// Method to spawn an specific object of the queue public void spawnSpecificFromPool(GameObject go, Vector3 position, Quaternion rotation) { go.transform.position = position; go.transform.rotation = rotation; go.SetActive(true); // We call an specific method of an interface to make sure the start method of the reused objects works IPooledObject pooledObj = go.GetComponent <IPooledObject>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } }
/// <summary> /// Called when the first scene is loaded. /// </summary> internal override void SingletonAwake() { // Cache the transform everything will be pooled to poolingParent = transform; // Preload everything IPooledObject clone = null; foreach (IPooledObject preloadObject in objectsToPreload) { clone = GetInstance(preloadObject); clone.gameObject.SetActive(false); } }
/// <summary> Spawns a GameObject taken from a pool to a specific position and rotation in world or local space and sets it as a child of a transform. </summary> public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation, Transform parent, bool instantiateInWorldSpace) { if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag " + tag + " doesn't exist."); return(null); } GameObject objectToSpawn = poolDictionary[tag].Dequeue(); if (instantiateInWorldSpace) { objectToSpawn.SetActive(true); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; if (parent != null) { objectToSpawn.transform.SetParent(parent); } } else if (parent != null) { objectToSpawn.SetActive(true); objectToSpawn.transform.SetParent(parent); objectToSpawn.transform.localPosition = position; objectToSpawn.transform.localRotation = rotation; } else { objectToSpawn.SetActive(true); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; if (parent != null) { objectToSpawn.transform.SetParent(parent); } } IPooledObject pooledObj = objectToSpawn.GetComponent <IPooledObject>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn); }
public GameObject SpawnObject(string tag, Vector3 position, Quaternion rotation) { GameObject obj = poolDictionary[tag].Dequeue(); obj.SetActive(true); obj.transform.SetPositionAndRotation(position, rotation); IPooledObject pooledObject = obj.GetComponent <IPooledObject>(); if (pooledObject != null) { pooledObject.OnObjectSpawn(); } poolDictionary[tag].Enqueue(obj); return(obj); }
public PoolSet(GameObject original) { IPooledObject script = original.GetComponent <IPooledObject>(); if (script != null) { script.OriginalPrefab = original; containsPoolScript = true; } else { containsPoolScript = false; } allClonedInstances = new Dictionary <GameObject, IPooledObject>(); }
public void SetTileOnFire(Vector3Int tilePosition, TileData data) { //Fire newFire = Instantiate(firePrefab); GameObject newFire = ObjectPoolerUpgraded.Instance.SpawnFromPool("Fire", new Vector3(0f, 0f, 0f), Quaternion.identity); //newFire.StartBurning(tilePosition, data, this); IPooledObject pooledObject = newFire.GetComponent <IPooledObject>(); pooledObject.OnObjectSpawnBurn(tilePosition, data, this); newFire.transform.position = map.GetCellCenterWorld(tilePosition); map.SetTile(tilePosition, blackTile); activeFires.Add(tilePosition); }
private void ObjectGetsDisabled(IPooledObject obj) { Push(obj); }
/// <summary> /// When ever a IPooledObject is done being used wit it call this function /// which takes it from the allocated queue (if it's there) and adds it to /// the deallocated queue. /// </summary> /// <param name="iPooledObject">The item you want to deallocate.</param> public void Deallocate(IPooledObject iPooledObject) { iPooledObject.OnDeallocated(); iPooledObject.gameObject.SetActive(false); m_DeallocatedStack.Push(iPooledObject); }
public static void ReleaseInstanceToPool(IPooledObject instance) { instance.IsBusy = false; }