-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.cs
More file actions
127 lines (107 loc) · 3.51 KB
/
Copy pathObjectPool.cs
File metadata and controls
127 lines (107 loc) · 3.51 KB
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
using System.Collections;
using UnityEngine;
namespace PLib.Pool
{
/// <summary>
/// Light object pool pattern realization
/// </summary>
public sealed class ObjectPool<T> : IEnumerable where T : UnityEngine.Object
{
private T[] _pool;
private int _count;
private int _currentIndex = -1;
private GameObject _poolContainer;
public int Count => _count;
public GameObject ContainerObject => _poolContainer;
public T Current
{
get { return _pool[_currentIndex < 0 ? 0 : _currentIndex ]; }
}
public IEnumerator GetEnumerator() => new ObjectIterator(this);
/// <summary>
/// Create pool container as parrent and instantiate items by count
/// </summary>
/// <param name="poolName">Default name: ObjectPoolContainer</param>
public ObjectPool(T objectPrefab, int count, string poolName = "PoolContainer")
{
_count = count;
_pool = new T[count];
_poolContainer = new GameObject();
if (poolName = "PoolContainer")
_poolContainer.name = objectPrefab.name + poolName;
else
_poolContainer.name = poolName;
Init(objectPrefab, _poolContainer.transform);
}
/// <summary>
/// Instantiate items by count, use specified Transform as parrent
/// </summary>
public ObjectPool(T objectPrefab, int count, Transform parent)
{
_count = count;
_pool = new T[count];
_poolContainer = parent.gameObject;
Init(objectPrefab, parent);
}
private void Init(T objectPrefab, Transform parent)
{
_currentIndex = -1;
for (int i = 0; i < _count; i++)
{
_pool[i] = Object.Instantiate(objectPrefab, parent);
}
}
/// <summary>
/// Return item by id and set as current item <br/>
/// id > pool.Count : return last item <br/>
/// id < 0 : return first (id: 0) item
/// </summary>
public T GetByIndex(int index)
{
index = index < 0 ? -1 : index >= _count ? _count - 1 : index;
_currentIndex = index;
return Current;
}
/// <summary>
/// Return next item <br/>
/// Cyclically
/// </summary>
public T GetNext()
{
_currentIndex++;
if (_currentIndex >= _count) _currentIndex = 0;
return Current;
}
/// <summary>
/// Clear pool, destroy pool container
/// </summary>
public void Clear()
{
if (_poolContainer)
Object.Destroy(_poolContainer);
_pool = null;
_count = 0;
_currentIndex = -1;
}
private class ObjectIterator : IEnumerator
{
private ObjectPool<T> _objectPool;
private int _currentIndex = -1;
public ObjectIterator(ObjectPool<T> objectPool)
{
_objectPool = objectPool;
}
public object Current => _objectPool._pool[_currentIndex];
public bool MoveNext()
{
_currentIndex++;
if(_currentIndex < _objectPool._count) return true;
else return false;
}
public void Reset()
{
_currentIndex = -1;
}
}
}
}