-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectPool.cs
90 lines (77 loc) · 2.43 KB
/
ObjectPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections.Generic;
using UnityEngine;
namespace FE.ObjectPool
{
public class ObjectPool<T> where T : MonoBehaviour
{
private readonly int expand_step = 10;
private readonly Queue<T> m_items;
private readonly Transform m_parent;
private readonly T m_prefab;
public ObjectPool(T prefab, int count)
{
m_items = new Queue<T>(count);
m_prefab = prefab;
InitializePool(prefab, count);
}
public ObjectPool(T prefab, int count, Transform parent)
{
m_items = new Queue<T>(count);
m_prefab = prefab;
m_parent = parent;
InitializePool(prefab, count, parent);
}
public ObjectPool(T prefab, int count, int expandStep, Transform parent)
{
m_items = new Queue<T>(count);
expand_step = expandStep;
m_prefab = prefab;
m_parent = parent;
InitializePool(prefab, count, parent);
}
public T Get()
{
if (m_items.Count == 0)
{
int c = 0;
if (expand_step == 0)
{
Debug.LogWarning("Expansion step is 0, pool is empty!");
}
else
{
while (c < expand_step)
{
InstantiateInstance(m_prefab, m_parent);
c++;
}
}
}
return m_items.Dequeue();
}
public void Return(T t)
{
m_items.Enqueue(t);
}
private void InitializePool(T prefab, int count)
{
for (int i = 0; i < count; i++) InstantiateInstance(prefab);
}
private void InitializePool(T prefab, int count, Transform parent)
{
for (int i = 0; i < count; i++) InstantiateInstance(prefab, parent);
}
private void InstantiateInstance(T prefab)
{
T instance = Object.Instantiate(prefab);
m_items.Enqueue(instance);
instance.gameObject.SetActive(false);
}
private void InstantiateInstance(T prefab, Transform parent)
{
T instance = Object.Instantiate(prefab, parent, true);
m_items.Enqueue(instance);
instance.gameObject.SetActive(false);
}
}
}