-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.cs
More file actions
38 lines (31 loc) · 884 Bytes
/
ObjectPool.cs
File metadata and controls
38 lines (31 loc) · 884 Bytes
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
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
[Header("Pool Settings")]
[SerializeField] private PoolableObject objectPrefab;
private readonly Queue<PoolableObject> pool = new Queue<PoolableObject>();
public GameObject GetObject()
{
PoolableObject obj;
if (pool.Count > 0)
{
obj = pool.Dequeue();
}
else
{
obj = Instantiate(objectPrefab, transform);
}
obj.gameObject.SetActive(true);
obj.pool = this;
obj.InitializePoolObject();
return obj.gameObject;
}
public void ReturnObject(PoolableObject obj)
{
if (obj == null) return;
obj.DeactivatePoolObject();
obj.gameObject.SetActive(false);
pool.Enqueue(obj);
}
}