-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pool.cs
152 lines (136 loc) · 5.08 KB
/
Pool.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TC;
using NUnit.Framework;
namespace TC
{
/// <summary>
/// Class for basic pooling.
/// Will need to be used with PoolableObject.
/// </summary>
public class Pool
{
private const int PoolSizeNoLimit = -1;
// Object to be pooled.
private readonly PoolableObject poolableObject;
private readonly Queue<PoolableObject> pool;
/// <summary>
/// Initializes a new instance of the <see cref="Pool"/> class.
/// </summary>
/// <param name="_poolableObject">Poolable object.</param>
/// <param name="_maxPoolSize">Max pool size.</param>
public Pool(PoolableObject _poolableObject, int _maxPoolSize = PoolSizeNoLimit)
{
poolableObject = _poolableObject;
pool = _maxPoolSize == PoolSizeNoLimit
? new Queue<PoolableObject>()
: new Queue<PoolableObject>(_maxPoolSize);
}
/// <summary>
/// Gets the pool instance from queue or creates new pool instance.
/// </summary>
/// <returns>The or create.</returns>
/// <param name="_parent">Parent.</param>
/// <param name="_localPos">Local position.</param>
public PoolableObject GetOrCreate(Transform _parent, Vector3 _localPos = default(Vector3))
{
PoolableObject poolObj = GetPoolableObject(_parent);
if (poolObj == null) poolObj = CreatePoolableObject(_parent);
// Initialize poolable object, no matter get or create.
poolObj.gameObject.SetActive(true);
poolObj.transform.localPosition = _localPos;
return poolObj;
}
/// <summary>
/// Returns pool instance to the pool.
/// </summary>
/// <param name="_poolableObject">Poolable object.</param>
public void Return2Pool(PoolableObject _poolableObject)
{
pool.Enqueue(_poolableObject);
_poolableObject.gameObject.SetActive(false);
}
/// <summary>
/// Peeks the pool object and receives peeked object.
/// By calling this method, PoolableObject still remains in Pool.
///
/// Will return null if _createIfPoolIsEmpty is pool is null and false.
/// </summary>
/// <param name="_createIfPoolIsEmpty">If set to <c>true</c> create if pool is empty.</param>
/// <param name="_parent">Parent.</param>
public PoolableObject Peek(bool _createIfPoolIsEmpty = false, Transform _parent = null)
{
if(!IsPoolObjectAvailable())
{
if(!_createIfPoolIsEmpty)
{
return null;
}
// Create pool object for peeking
PoolableObject poolObj = CreatePoolableObject(null);
if(_parent != null)
{
poolObj.transform.SetParent(_parent);
}
// Return this to pool for peeking.
Return2Pool(poolObj);
}
return pool.Peek();
}
/// <summary>
/// Gets all objects in pool.
/// </summary>
/// <returns>The all objects in pool.</returns>
public List<T> GetAllObjectsInPool<T>(Transform _parent) where T : PoolableObject
{
List<T> result = new List<T>();
while(IsPoolObjectAvailable())
{
result.Add(GetPoolableObject(_parent) as T);
}
return result;
}
/// <summary>
/// Clear every PoolableObject within Pool.
/// </summary>
public void Clear()
{
while(IsPoolObjectAvailable())
{
GameObject.Destroy(GetPoolableObject(null).gameObject);
}
}
/// <summary>
/// Gets the poolable object from queue.
/// </summary>
/// <returns>The poolable object.</returns>
/// <param name="_parent">Parent.</param>
private PoolableObject GetPoolableObject(Transform _parent)
{
if (!IsPoolObjectAvailable()) return null;
PoolableObject poolObj = pool.Dequeue();
poolObj.transform.SetParent(_parent);
return poolObj;
}
/// <summary>
/// Creates new poolable object.
/// </summary>
/// <returns>The poolable object.</returns>
/// <param name="_parent">Parent.</param>
private PoolableObject CreatePoolableObject(Transform _parent)
{
PoolableObject poolObj = PoolableObject.Instantiate(this, poolableObject);
poolObj.transform.SetParent(_parent);
return poolObj;
}
/// <summary>
/// Determines whether reusable pool object is available in pool.
/// </summary>
/// <returns><c>true</c> if this instance is pool object available; otherwise, <c>false</c>.</returns>
private bool IsPoolObjectAvailable()
{
return pool.Count > 0;
}
}
}