49 lines
1.0 KiB
C#
49 lines
1.0 KiB
C#
![]() |
|
|||
|
#if UNITY_EDITOR
|
|||
|
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace UguiToolkit.Editor
|
|||
|
{
|
|||
|
public class ObjectPool<T> where T : new()
|
|||
|
{
|
|||
|
public int initialSize = 10; // <20><>ʼ<EFBFBD><CABC>С
|
|||
|
private Queue<T> pool;
|
|||
|
|
|||
|
public ObjectPool(int initialSize)
|
|||
|
{
|
|||
|
this.initialSize = initialSize;
|
|||
|
this.pool = new Queue<T>(initialSize);
|
|||
|
// Ԥ<>ȴ<EFBFBD><C8B4><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD>
|
|||
|
for (int i = 0; i < initialSize; i++)
|
|||
|
{
|
|||
|
T obj = new T();
|
|||
|
pool.Enqueue(obj);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
public T GetObject()
|
|||
|
{
|
|||
|
if (pool.Count > 0)
|
|||
|
{
|
|||
|
T obj = pool.Dequeue();
|
|||
|
return obj;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>п<EFBFBD><D0BF>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2B4B4BD>µĶ<C2B5><C4B6><EFBFBD>
|
|||
|
T obj = new T();
|
|||
|
return obj;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>ͷŶ<CDB7><C5B6><EFBFBD>
|
|||
|
public void ReturnObject(T obj)
|
|||
|
{
|
|||
|
pool.Enqueue(obj);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endif
|