38 lines
789 B
C#
38 lines
789 B
C#
using System.Collections.Generic;
|
|
|
|
public class GameObjectPool<T> where T : class
|
|
{
|
|
short count;
|
|
public delegate T Func();
|
|
Func create_fn;
|
|
Queue<T> objects;
|
|
public GameObjectPool(short count, Func fn)
|
|
{
|
|
this.count = count;
|
|
this.create_fn = fn;
|
|
this.objects = new Queue<T>(this.count);
|
|
allocate();
|
|
}
|
|
void allocate()
|
|
{
|
|
for (int i = 0; i < this.count; i++)
|
|
{
|
|
this.objects.Enqueue(this.create_fn());
|
|
}
|
|
}
|
|
public T pop()
|
|
{//dequeue 꺼내는 부분
|
|
if (this.objects.Count <= 0)
|
|
{
|
|
allocate();
|
|
}
|
|
return this.objects.Dequeue();
|
|
}
|
|
public void push(T obj)
|
|
{//enqueue 쌓는부분
|
|
this.objects.Enqueue(obj);
|
|
}
|
|
}
|
|
|
|
|