From 26b316d4315f94de4d976937343da04d30bb773f Mon Sep 17 00:00:00 2001 From: hyunwoo Date: Wed, 16 Aug 2023 18:50:56 +0900 Subject: [PATCH] Singleton --- Assets/Singleton.meta | 8 ++ Assets/Singleton/Scripts.meta | 8 ++ Assets/Singleton/Scripts/UnitySingleton.cs | 93 +++++++++++++++++++ .../Singleton/Scripts/UnitySingleton.cs.meta | 11 +++ 4 files changed, 120 insertions(+) create mode 100644 Assets/Singleton.meta create mode 100644 Assets/Singleton/Scripts.meta create mode 100644 Assets/Singleton/Scripts/UnitySingleton.cs create mode 100644 Assets/Singleton/Scripts/UnitySingleton.cs.meta diff --git a/Assets/Singleton.meta b/Assets/Singleton.meta new file mode 100644 index 0000000..d0da518 --- /dev/null +++ b/Assets/Singleton.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 46c57a5edc113954c92ea81e20be4682 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Singleton/Scripts.meta b/Assets/Singleton/Scripts.meta new file mode 100644 index 0000000..02c2712 --- /dev/null +++ b/Assets/Singleton/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 79590437eab636d45b613b2bcda6a904 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Singleton/Scripts/UnitySingleton.cs b/Assets/Singleton/Scripts/UnitySingleton.cs new file mode 100644 index 0000000..1df58d5 --- /dev/null +++ b/Assets/Singleton/Scripts/UnitySingleton.cs @@ -0,0 +1,93 @@ +using UnityEngine; + +namespace FirstVillain.Singleton +{ + public class UnitySingleton : MonoBehaviour where T : UnityEngine.Component + { + private static T _instance = null; + + public static T Instance + { + get + { + if (_instance == null) + { + string name = (typeof(T)).ToString(); + _instance = new GameObject(name).AddComponent(); + } + + return _instance; + } + } + private void Awake() + { + if (_instance != null && _instance != this) + { + Destroy(gameObject); + return; + } + else + { + _instance = this as T; + } + + DontDestroyOnLoad(gameObject); + + AwakeSingleton(); + } + + private void OnDestroy() + { + if (_instance == this) + { + _instance = null; + } + } + + protected virtual void AwakeSingleton() + { } + } + public class UnitySingletonOnce : MonoBehaviour where T : UnityEngine.Component + { + private static T _instance = null; + + public static T Instance + { + get + { + if (_instance == null) + { + string name = (typeof(T)).ToString(); + _instance = new GameObject(name).AddComponent(); + } + + return _instance; + } + } + private void Awake() + { + if (_instance != null && _instance != this) + { + Destroy(gameObject); + return; + } + else + { + _instance = this as T; + } + + AwakeSingleton(); + } + + private void OnDestroy() + { + if (_instance == this) + { + _instance = null; + } + } + + protected virtual void AwakeSingleton() + { } + } +} diff --git a/Assets/Singleton/Scripts/UnitySingleton.cs.meta b/Assets/Singleton/Scripts/UnitySingleton.cs.meta new file mode 100644 index 0000000..85e701e --- /dev/null +++ b/Assets/Singleton/Scripts/UnitySingleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 777a878dd9f43e349ac20fcd7e63dc31 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: