/// <summary> /// Create a new pool for the model /// The model has to be new, its instanceId will /// be used to create a dictionary entry /// </summary> /// <param name="model">the model of the pool</param> /// <param name="poolSize">The number of element in the pool</param> /// <param name="expandSize">By how many the pool has to increase</param> /// <returns>The new pool</returns> public UnityPool AddPool(UnityPoolObject model, uint poolSize, uint expandSize = 1) { UnityPool newPool = InstanciatePool(model, expandSize); newPool.SetSize(poolSize); _table.Add(model.GetInstanceID(), newPool); return(newPool); }
/// <summary> /// Construct the pool and initialize it /// </summary> /// <param name="model">The model for all the instances</param> /// <param name="expandSize">by how many the pool is increase if there is not enough place </param> public void Construct(UnityPoolObject model, uint expandSize) { _model = model; _expandSize = (int)expandSize; name = "Pool (" + _model.name + ")"; Resources = new List <UnityResource>(); _firstFreeResource = 0; _lastBusyResource = 0; }
/// <summary> /// Instanciate a new pool /// </summary> /// <param name="model">The model of the new pool</param> /// <param name="expandSize">By how many the pool has to increase</param> /// <returns></returns> private UnityPool InstanciatePool(UnityPoolObject model, uint expandSize) { GameObject poolGameObject = new GameObject(); poolGameObject.transform.parent = gameObject.transform; UnityPool pool = poolGameObject.AddComponent <UnityPool>(); pool.transform.parent = gameObject.transform; pool.Construct(model, expandSize); return(pool); }
/// <summary> /// Create a new resource into the pool /// </summary> /// <returns>The new resource</returns> private UnityResource CreateResource() { UnityPoolObject newPoolObject = Instantiate <UnityPoolObject>(_model); newPoolObject.transform.SetParent(gameObject.transform); newPoolObject.name = _model.name + " " + Resources.Count; newPoolObject.Pool = this; newPoolObject.gameObject.SetActive(false); newPoolObject.transform.position = StoredPosition; return(new UnityResource(newPoolObject)); }
/// <summary> /// Try to release the given resource /// </summary> /// <param name="poolObject">The PoolObject which contains the resource</param> public void ReleaseResource(UnityPoolObject poolObject) { if (Resources[poolObject.IndexInPool].PoolObject == poolObject) { if (Resources[poolObject.IndexInPool].IsUsed) { SetResourceState(false, poolObject.IndexInPool); } else { Debug.LogError("Try to destroy an unused object in the " + name + ".\n" + "It may be an error in the poolInstanceId (" + poolObject.IndexInPool + ")"); } } else { Debug.LogError("Try to destroy a different object in the " + name + ".\n" + "It may be an error in the poolInstanceId (" + poolObject.IndexInPool + ")"); } }
/// <summary> /// Check if there is already a pool with this model /// </summary> /// <param name="model"></param> /// <returns>True if there is one pool with this model, false otherwise</returns> public bool Contains(UnityPoolObject model) { return(_table.ContainsKey(model.GetInstanceID())); }
public UnityResource(UnityPoolObject poolObject) { this.PoolObject = poolObject; IsUsed = false; }