public bool DespawnPool(string name) { AP_Pool childScript = null; if (poolRef.ContainsKey(name) == true) { // reference already created childScript = poolRef[name]; } else { // ref not yet created childScript = MakePoolRef(name); // create ref } if (childScript == null) { // pool not found return(false); } else { for (int i = 0; i < childScript.masterPool.Count; i++) { childScript.Despawn(childScript.masterPool[i].obj, childScript.masterPool[i].refScript); } return(true); } }
public bool RemovePool(string name) { bool result = false; AP_Pool childScript = null; if (poolRef.ContainsKey(name) == true) { // reference already created childScript = poolRef[name]; } else { // ref not yet created childScript = MakePoolRef(name); // create ref } if (childScript == null) { // pool not found return(false); } else { result = DespawnPool(name); Destroy(childScript.gameObject); poolRef.Remove(name); return(result); } }
public GameObject Spawn(string name, Transform parent, int?child, Vector3 pos, Quaternion rot, bool usePosRot) { CheckDict(); if (poolRef.ContainsKey(name) == true) { // reference already created if (poolRef[name] != null) { // make sure pool still exsists return(poolRef[name].Spawn(parent, child, pos, rot, usePosRot)); // create spawn } else { // pool no longer exsists poolRef.Remove(name); // remove reference return(null); } } else { // ref not yet created AP_Pool childScript = MakePoolRef(name); // create ref if (childScript == null) { // ref not found return(null); } else { return(childScript.Spawn(parent, child, pos, rot, usePosRot)); // create spawn } } }
AP_Pool MakePoolRef(string name) { // attempt to create and return script reference for (int i = 0; i < transform.childCount; i++) { AP_Pool childScript = transform.GetChild(i).GetComponent <AP_Pool>(); if (childScript && name.Equals(childScript.poolBlock.prefab.name)) { poolRef.Add(name, childScript); return(childScript); } } // Debug.Log( obj.name + ": Tried to reference object pool, but no matching pool was found." ); return(null); }
public int GetAvailableCount(string name) { AP_Pool childScript = null; if (poolRef.ContainsKey(name) == true) { // reference already created childScript = poolRef[name]; } else { // ref not yet created childScript = MakePoolRef(name); // create ref } if (childScript == null) { // pool not found return(0); } else { return(childScript.pool.Count); } }
public void CreatePool(GameObject prefab, int size, int maxSize, AP_enum.EmptyBehavior emptyBehavior, AP_enum.MaxEmptyBehavior maxEmptyBehavior) { GameObject obj = new GameObject("Object Pool"); obj.transform.SetParent(transform); obj.transform.localPosition = Vector3.zero; obj.transform.localRotation = Quaternion.identity; AP_Pool script = obj.AddComponent <AP_Pool>(); if (Application.isPlaying == true) { obj.name = prefab.name; script.poolBlock.size = size; script.poolBlock.maxSize = maxSize; script.poolBlock.emptyBehavior = emptyBehavior; script.poolBlock.maxEmptyBehavior = maxEmptyBehavior; script.poolBlock.prefab = prefab; if (prefab) { MakePoolRef(prefab.name); } } }