-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectPooler.cs
84 lines (67 loc) · 2.69 KB
/
ObjectPooler.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ObjectPooler : MonoBehaviour
{
public static List<PooledObjectInfo> pooledObjects = new List<PooledObjectInfo>();
public static T SpawnObject<T>(T obj, Vector3 position, Quaternion rotation, Transform parent = null) where T : Component
{
return SpawnObject(obj.gameObject, position, rotation, parent: parent).GetComponent<T>();
}
public static GameObject SpawnObject(GameObject obj, Vector3 position, Quaternion rotation, Action<GameObject> OnComplete = null, Transform parent = null)
{
PooledObjectInfo pool = pooledObjects.Find(x => x.LookupString == obj.name);
if (pool == null)
{
GameObject poolParent = new GameObject(obj.name + " Pool");
pool = new PooledObjectInfo();
pool.LookupString = obj.name;
pool.parent = poolParent.transform;
pooledObjects.Add(pool);
}
GameObject spawnableObj = pool.InactiveObjects.FirstOrDefault();
if (spawnableObj != null)
{
spawnableObj.transform.position = position;
spawnableObj.transform.rotation = rotation;
spawnableObj.transform.SetParent(parent);
spawnableObj.SetActive(true);
pool.InactiveObjects.Remove(spawnableObj);
}
else
{
spawnableObj = Instantiate(obj, position, rotation, parent);
spawnableObj.name = obj.name;
}
OnComplete?.Invoke(spawnableObj);
return spawnableObj;
}
public static void DespawnObject(GameObject obj)
{
string objectName = obj.name.Replace("(Clone)", "");
PooledObjectInfo pool = pooledObjects.Find(x => x.LookupString == objectName);
if (pool == null)
{
pool = new PooledObjectInfo();
pool.LookupString = obj.name;
pooledObjects.Add(pool);
Debug.LogWarning($"Seems like you're trying to despawn a non pooled object: {objectName}. I've created a pool just in case but you need to instantiate it from the pool first.");
}
obj.transform.position = Vector3.zero;
obj.transform.rotation = Quaternion.identity;
obj.transform.SetParent(pool.parent);
obj.SetActive(false);
pool.InactiveObjects.Add(obj);
}
public static void DespawnObject(GameObject gameObject, float delay, MonoBehaviour mono)
{
mono.DelayedAction(null, delay, () => DespawnObject(gameObject));
}
}
public class PooledObjectInfo
{
public string LookupString;
public List<GameObject> InactiveObjects = new List<GameObject>();
public Transform parent;
}