-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObjectPool.cs
70 lines (60 loc) · 2.23 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
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// A type-safe, generic object pool. This object pool requires you to derive a class from it,
/// and specify the type of object to pool.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObjectPool<T> : MonoBehaviour where T : MonoBehaviour
{
[Tooltip("Prefab for this object pool")]
public T m_prefab;
[Tooltip("Size of this object pool")]
public int m_size;
// The list of free and used objects for tracking.
private List<T> m_freeList;
private List<T> m_usedList;
public void Awake()
{
m_freeList = new List<T>(m_size);
m_usedList = new List<T>(m_size);
// Instantiate the pooled objects and disable them.
for (var i = 0; i < m_size; i++)
{
var pooledObject = Instantiate(m_prefab, transform);
pooledObject.gameObject.SetActive(false);
m_freeList.Add(pooledObject);
}
}
/// <summary>
/// Returns an object from the pool. Returns null if there are no more objects free in the pool.
/// </summary>
/// <returns>Object of type T from the pool.</returns>
public T Get()
{
var numFree = m_freeList.Count;
if (numFree == 0)
return null;
// Pull an object from the end of the free list.
var pooledObject = m_freeList[numFree - 1];
m_freeList.RemoveAt(numFree - 1);
m_usedList.Add(pooledObject);
return pooledObject;
}
/// <summary>
/// Returns an object to the pool. The object must have been created by this ObjectPool.
/// </summary>
/// <param name="pooledObject">Object previously obtained from this ObjectPool</param>
public void ReturnObject(T pooledObject)
{
Debug.Assert(m_usedList.Contains(pooledObject));
// Put the pooled object back in the free list.
m_usedList.Remove(pooledObject);
m_freeList.Add(pooledObject);
// Reparent the pooled object to us, and disable it.
var pooledObjectTransform = pooledObject.transform;
pooledObjectTransform.parent = transform;
pooledObjectTransform.localPosition = Vector3.zero;
pooledObject.gameObject.SetActive(false);
}
}