Skip to content

Commit 501b7e2

Browse files
committed
optimizing the code and implementing singleton
1 parent 2df7042 commit 501b7e2

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33

4-
public class ObjectPooler : MonoBehaviour
4+
public class ObjectPooler : SceneSingleton<ObjectPooler>
55
{
66

77
public Dictionary<PooledObjectType, Queue<GameObject>> PoolDictionary;
@@ -10,13 +10,6 @@ public class ObjectPooler : MonoBehaviour
1010
private Dictionary<PooledObjectType, int> _poolIndexes = new Dictionary<PooledObjectType, int>();
1111
private Dictionary<PooledObjectType, Transform> _poolMasters = new Dictionary<PooledObjectType, Transform>();
1212

13-
public static ObjectPooler Instance;
14-
15-
private void Awake()
16-
{
17-
Instance = this;
18-
}
19-
2013
private void Start()
2114
{
2215
PoolDictionary = new Dictionary<PooledObjectType, Queue<GameObject>>();
@@ -98,17 +91,21 @@ public void Despawn(GameObject obj)
9891
{
9992
PooledObjectType tag = obj.GetComponent<IPooledObject>().PoolType;
10093

101-
if (tag != null)
94+
if (tag != null && PoolDictionary.ContainsKey(tag))
10295
{
96+
97+
10398
PoolDictionary[tag].Enqueue(obj);
10499

105100
IPooledObject iPooledObj = obj.GetComponent<IPooledObject>();
106101
if (iPooledObj != null) iPooledObj.OnObjectDespawn();
107102
obj.SetActive(false);
103+
104+
108105
}
109106
else
110107
{
111-
Debug.LogWarning("Trying to despawn object which is not pooled !");
108+
Debug.LogError("Trying to despawn object which is not pooled !");
112109
}
113110

114111
}

Assets/CustomTools/Sıngleton.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using UnityEngine;
2+
3+
public class SceneSingleton<T> : MonoBehaviour
4+
where T : Component
5+
{
6+
static T s_instance = null;
7+
8+
public static T Instance
9+
{
10+
get
11+
{
12+
if (s_instance == null)
13+
{
14+
s_instance = Object.FindObjectOfType<T>();
15+
}
16+
17+
return s_instance;
18+
}
19+
}
20+
}

Assets/CustomTools/Sıngleton/SceneSingleton.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)