/// <summary> /// 将指定的对象回收到对象池。 /// </summary> /// <param name="obj">对象</param> public void Put(T obj) { if (AvailiableObjects.Contains(obj)) { return; } OnPut(obj); AvailiableObjects.Enqueue(obj); }
/// <summary> /// 将指定的对象回收到对象池。 /// </summary> /// <param name="obj">对象</param> public void Put(object obj) { var put = (T)obj; if (AvailiableObjects.Contains(put)) { return; } OnPut(put); AvailiableObjects.Enqueue(put); }
/// <summary> /// 清理所有缓存的对象。 /// </summary> public void Clear() { while (AvailiableObjects.Count > 0) { UnityEngine.Object.Destroy(AvailiableObjects.Dequeue()); } while (InUseObjects.Count > 0) { UnityEngine.Object.Destroy(InUseObjects.Dequeue()); } }
/// <summary> /// 从对象池中获取一个对象。 /// </summary> /// <returns>可用的对象</returns> public T Get() { T ret; if (AvailiableObjects.Count != 0) { ret = AvailiableObjects.Dequeue(); } else if (InUseObjects.Count == Capacity) { ret = InUseObjects.Dequeue(); OnPut(ret); } else { ret = Create(); } OnGet(ret); InUseObjects.Enqueue(ret); return(ret); }