public PoolableBehaviour GetObjectFromPool(PoolableBehaviour prefab) { int ID = prefab.GetInstanceID(); // If there is no key in dic if (!objectPool.ContainsKey(ID)) { CreatePool(prefab, _InitialPoolSize); } List <PoolableBehaviour> objectPoolList = objectPool[ID]; for (int i = 0; i < objectPoolList.Count; ++i) { IncreaseIndexPool(ID); // check if it is active or not if (!objectPoolList[indexPool[ID]].gameObject.activeInHierarchy) { PoolableBehaviour obj = objectPoolList[indexPool[ID]]; obj.gameObject.SetActive(true); return(obj); } } PoolableBehaviour tmp = AddObjectToPool(ID, prefab); tmp.gameObject.SetActive(true); return(tmp); }
// Get a pooled instance of the specified prefab, creating pool or instances as necessary public PoolableBehaviour GetPooledObject(PoolableBehaviour prefab) { if (!poolMap.ContainsKey(prefab)) { poolMap[prefab] = new ObjectPool(prefab, defaultPoolSize); Debug.LogWarning("Creating pool for uninitialized prefab: " + prefab + " with initial size of " + defaultPoolSize); } return poolMap[prefab].GetFromPool(); }
private PoolableBehaviour AddObjectToPool(int ID, PoolableBehaviour prefab) { PoolableBehaviour obj = Instantiate(prefab); obj.gameObject.SetActive(false); objectPool[ID].Add(obj); return(obj); }
// Get a pooled instance of the specified prefab, creating pool or instances as necessary public PoolableBehaviour GetPooledObject(PoolableBehaviour prefab) { if (!poolMap.ContainsKey(prefab)) { poolMap[prefab] = new ObjectPool(prefab, defaultPoolSize); Debug.LogWarning("Creating pool for uninitialized prefab: " + prefab + " with initial size of " + defaultPoolSize); } return(poolMap[prefab].GetFromPool()); }
public void testAutoPoolManagerCreation() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); // request object without pre-creating PoolManager PoolableBehaviour obj = PoolManager.Instance.GetPooledObject(prefab); Assert.NotNull(obj, "Retrieved null object from pool"); Assert.AreNotEqual(prefab, obj, "Received prefab itself from pool"); }
// create a single object and configure for this pool private PoolableBehaviour createObject() { totalCreated++; PoolableBehaviour created = (PoolableBehaviour)Object.Instantiate(prefab); created.transform.parent = parent; created.SetOwner(this); created.Deactivate(); return(created); }
public void testEmptyPool() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); int initialSize = 0; int incrementSize = 1; ObjectPool pool = new ObjectPool(prefab, initialSize, incrementSize); PoolableBehaviour retrieved = pool.GetFromPool(); Assert.IsNotNull(retrieved); }
/// <summary> /// Make the tentacle length shorter /// </summary> /// <param name="times">Define how short it should be</param> public void Shorten(int times) { // print("shortening " + times + " times"); for (int i = 0; i < times; i++) { if (armParts.Count > 1) { // print("destroying arm segment"); PoolableBehaviour target = armParts[armParts.Count - 1]; armParts.Remove(target); target.SetActive(false); } } }
public void CreatePool(PoolableBehaviour prefab, int poolSize) { // different for each instance int ID = prefab.GetInstanceID(); objectPool[ID] = new List <PoolableBehaviour>(); indexPool[ID] = 0; for (int i = 0; i < poolSize; ++i) { PoolableBehaviour obj = Instantiate(prefab); obj.gameObject.SetActive(false); objectPool[ID].Add(obj); } }
/// <summary> /// Spawn the tentacle parts /// </summary> /// <param name="spawnPoint">Tentacle root location</param> /// <param name="direction">Direction of the arm</param> private void Extend(Transform spawnPoint, Vector3 direction) { if (!autoExtend) { return; } //print("tenatcle Extend"); //TentacleSegment currentSegment = Instantiate(armPrefab, GetSegmentLocation(spawnPoint.position, direction), spawnPoint.rotation); PoolableBehaviour currentSegment = PoolManager.instance.GetObjectFromPool(armPrefab); // reset the position of the obj currentSegment.transform.position = Vector3.zero; armParts.Add(currentSegment); (currentSegment as TentacleSegment).rootTentacle = this; }
public ObjectPool(PoolableBehaviour prefab, int initialSize, int incrementSize) { this.prefab = prefab; if (incrementSize < 1) { this.incrementSize = 1; Debug.LogError("Non-positive increment requested, defaulting to increment size of 1"); } else { this.incrementSize = incrementSize; } if (initialSize < 0) { initialSize = 0; Debug.LogError("Negative initial size requested, defaulting to size of 0"); } pool = new Stack<PoolableBehaviour>(initialSize); totalCreated = 0; CreateAndAddToPool(initialSize); }
public void testPoolReuse() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); ObjectPool pool = new ObjectPool(prefab, 1, 1); // pool should never return the prefab itself PoolableBehaviour retrieved = pool.GetFromPool(); Assert.AreNotEqual(prefab, retrieved, "Prefab itself returned from pool"); // pool should not return an in-use object PoolableBehaviour retrieved2 = pool.GetFromPool(); Assert.AreNotEqual(retrieved, retrieved2, "Pool returned in-use object twice"); // object returned to pool should be retrieved from pool retrieved.ReturnToPool(); PoolableBehaviour retrieved3 = pool.GetFromPool(); Assert.AreEqual(retrieved, retrieved3, "Pool did not re-use returned object"); }
public ObjectPool(PoolableBehaviour prefab, int initialSize, int incrementSize) { this.prefab = prefab; if (incrementSize < 1) { this.incrementSize = 1; Debug.LogError("Non-positive increment requested, defaulting to increment size of 1"); } else { this.incrementSize = incrementSize; } if (initialSize < 0) { initialSize = 0; Debug.LogError("Negative initial size requested, defaulting to size of 0"); } pool = new Stack <PoolableBehaviour>(initialSize); totalCreated = 0; CreateAndAddToPool(initialSize); }
public void Return(PoolableBehaviour c) { c.gameObject.SetActive(false); objectStack.Push((T)c); }
public ObjectPool(PoolableBehaviour prefab, int initialSize) : this(prefab, initialSize, initialSize) { }
// return an object to this pool public void ReturnToPool(PoolableBehaviour obj) { pool.Push(obj); //Debug.Log("Created new object"); }