From 501b7e2e6992859ba771d5c0e0b848670b7cdd4d Mon Sep 17 00:00:00 2001 From: Ertan Turan Date: Mon, 21 Sep 2020 22:01:21 +0300 Subject: [PATCH] optimizing the code and implementing singleton --- .../Scripts/ObjectPool/ObjectPooler.cs | 17 +++++++--------- "Assets/CustomTools/S\304\261ngleton.meta" | 8 ++++++++ .../S\304\261ngleton/SceneSingleton.cs" | 20 +++++++++++++++++++ .../S\304\261ngleton/SceneSingleton.cs.meta" | 11 ++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 "Assets/CustomTools/S\304\261ngleton.meta" create mode 100644 "Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs" create mode 100644 "Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs.meta" diff --git a/Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs b/Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs index 81f5406..e2c24df 100644 --- a/Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs +++ b/Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using UnityEngine; -public class ObjectPooler : MonoBehaviour +public class ObjectPooler : SceneSingleton { public Dictionary> PoolDictionary; @@ -10,13 +10,6 @@ public class ObjectPooler : MonoBehaviour private Dictionary _poolIndexes = new Dictionary(); private Dictionary _poolMasters = new Dictionary(); - public static ObjectPooler Instance; - - private void Awake() - { - Instance = this; - } - private void Start() { PoolDictionary = new Dictionary>(); @@ -98,17 +91,21 @@ public void Despawn(GameObject obj) { PooledObjectType tag = obj.GetComponent().PoolType; - if (tag != null) + if (tag != null && PoolDictionary.ContainsKey(tag)) { + + PoolDictionary[tag].Enqueue(obj); IPooledObject iPooledObj = obj.GetComponent(); if (iPooledObj != null) iPooledObj.OnObjectDespawn(); obj.SetActive(false); + + } else { - Debug.LogWarning("Trying to despawn object which is not pooled !"); + Debug.LogError("Trying to despawn object which is not pooled !"); } } diff --git "a/Assets/CustomTools/S\304\261ngleton.meta" "b/Assets/CustomTools/S\304\261ngleton.meta" new file mode 100644 index 0000000..7f18ac7 --- /dev/null +++ "b/Assets/CustomTools/S\304\261ngleton.meta" @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1d890cd2fec112f4c98042d12d9a7d68 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git "a/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs" "b/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs" new file mode 100644 index 0000000..085c608 --- /dev/null +++ "b/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs" @@ -0,0 +1,20 @@ +using UnityEngine; + +public class SceneSingleton : MonoBehaviour + where T : Component +{ + static T s_instance = null; + + public static T Instance + { + get + { + if (s_instance == null) + { + s_instance = Object.FindObjectOfType(); + } + + return s_instance; + } + } +} \ No newline at end of file diff --git "a/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs.meta" "b/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs.meta" new file mode 100644 index 0000000..869f833 --- /dev/null +++ "b/Assets/CustomTools/S\304\261ngleton/SceneSingleton.cs.meta" @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eed6a11e42e698146a686d15c175f26f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: