// Spawn Object and set its pool change its parent to poolParent then disable it IObjectPoolAble SpawnObject( ) { IObjectPoolAble item = GameObject.Instantiate(pooledObject, poolParent).GetComponent <IObjectPoolAble> ( ); item.Pool = this; item.gameObject.SetActive(false); return(item); }
/// <summary>Return IObjectPoolItem</summary> /// <remarks>Need to convert to type which user need /// e.g. Bullets inherit from Mono,IObjectPoolItem /// if we ask for a bullet we need to convert type to bullet</remarks> /// <param name="data">you can add some data to object when it call init</param> /// <example><code>Bullet bullet = Bullets.GetPooledObject ( ) as Bullet;</code></example> public IObjectPoolAble GetPooledObject <T> (T data) { if (poolObjects.Count != 0) { IObjectPoolAble item = poolObjects.Dequeue( ); item.Init <T> (data); item.gameObject.SetActive(true); return(item); } if (IsGrow) { IObjectPoolAble item = SpawnObject( ); item.Init <T> (data); item.gameObject.SetActive(true); return(item); } return(null); }
/// <summary>Recycle Object to Pooling again</summary> /// <param name="item">which item will be Recycle to ObjectPooling</param> public void RecycleObject(IObjectPoolAble item) { poolObjects.Enqueue(item); item.gameObject.SetActive(false); }