public bool Return(GameObject target) { if (target == null) { Debug.LogError("Destroy null"); return(false); } target.transform.parent = null; PoolObjectModel currentPoolObject = GetPoolObject(target); if (IsPoolObjectExist(target)) { if (!currentPoolObject.Available.Contains(target)) { currentPoolObject.Available.Push(target); SetActive(target, false); return(true); } } else { Debug.LogError("Direct destroy " + target.name); GameObject.Destroy(target); return(true); } return(false); }
private PoolObjectModel GetPoolObject(string prefabName) { if (!prefabName.Contains("(Cached)")) { prefabName = prefabName + "(Cached)"; } PoolObjectModel poolObjectToReturn = null;// new PoolObjectModel(); m_PoolObjectList.TryGetValue(prefabName, out poolObjectToReturn); return(poolObjectToReturn); }
public int NumAvailable(GameObject prefab) { PoolObjectModel poolObject = GetPoolObject(prefab); if (poolObject == null) { return(0); } else { return(poolObject.Available.Count); } }
public PoolObjectModel AddNewPoolObject(GameObject prefab, uint initialCapacity) { //Debug.Log("Add Old object" + prefab.name); PoolObjectModel newPoolObject = new PoolObjectModel(); newPoolObject.Prefab = prefab; newPoolObject.PrefabName = prefab.name + "(Cached)"; newPoolObject.All = (initialCapacity > 0) ? new ArrayList((int)initialCapacity) : new ArrayList(); newPoolObject.Available = (initialCapacity > 0) ? new Stack((int)initialCapacity) : new Stack(); m_PoolObjectList.Add(newPoolObject.PrefabName, newPoolObject); return(newPoolObject); }
public AquireResult Acquire(GameObject prefab, Vector3 position, Quaternion rotation, bool setActive = true) { AquireResult result = new AquireResult(); GameObject go; PoolObjectModel currentPoolObject = new PoolObjectModel(); if (IsPoolObjectExist(prefab)) { currentPoolObject = GetPoolObject(prefab); } else { currentPoolObject = AddNewPoolObject(prefab, 4); } if (currentPoolObject.Available.Count == 0) { LogService.Logger.Log(LogService.LogType.LT_DEBUG, "GameObjPool Cache Missed"); go = GameObject.Instantiate(prefab, position, rotation) as GameObject; go.name = currentPoolObject.PrefabName; GameObject.DontDestroyOnLoad(go); currentPoolObject.All.Add(result); result.Pooled = false; } else { go = currentPoolObject.Available.Pop() as GameObject; // get the result’s transform and reuse for efficiency. // calling gameObject.transform is expensive. if (go == null) { Debug.LogError("Pool Has Bug!" + currentPoolObject.PrefabName); //result = GameObject.Instantiate(prefab, position, rotation) as GameObject; //result.name = currentPoolObject.Prefab.name; } Transform resultTrans = go.transform; resultTrans.position = position; resultTrans.rotation = rotation; if (setActive) { SetActive(go, true); } result.Pooled = true; } result.GameObject = go; return(result); }