-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.cs
More file actions
65 lines (58 loc) · 1.51 KB
/
ObjectPool.cs
File metadata and controls
65 lines (58 loc) · 1.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
using UnityEngine;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour
{
//public static ObjectPool SharedInstance;
public List<GameObject> pooledObjects;
public GameObject objectToPool;
public int amountToPool;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{
//SharedInstance = this;
pooledObjects = new List<GameObject>();
GameObject tmp;
for (int i = 0; i < amountToPool; i++)
{
tmp = Instantiate(objectToPool);
tmp.SetActive(false);
tmp.transform.parent = transform;
pooledObjects.Add(tmp);
}
}
/*
* Attempts to retreive an object from the object pool.
* Returns null if there is no object.
*/
public GameObject GetPooledObject()
{
for (int i = 0; i < amountToPool; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
return null;
}
public int GetCapacity()
{
return amountToPool;
}
public int GetStoredObjectCount()
{
int storage = 0;
for (int i = 0; i < amountToPool; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
storage++;
}
}
return storage;
}
public int GetActiveObjectCount()
{
return GetCapacity() - GetStoredObjectCount();
}
}