From 8467fd9ef684844191c3325dd39c19370b2c19b6 Mon Sep 17 00:00:00 2001 From: Fcmz <2796744106@qq.com> Date: Tue, 28 Nov 2023 14:39:31 +0800 Subject: [PATCH] Add Singleton and MonoSingleton --- Assets/Scripts/GameManager.cs | 2 +- Assets/Scripts/MonoSingleton.cs | 63 +++++++++++++ Assets/Scripts/MonoSingleton.cs.meta | 12 +++ Assets/Scripts/SceneSingleton.meta | 8 ++ Assets/Scripts/Singleton.cs | 135 +++++++++++++++------------ Assets/Scripts/Singleton.cs.meta | 5 +- ProjectSettings/ProjectVersion.txt | 3 +- 7 files changed, 165 insertions(+), 63 deletions(-) create mode 100644 Assets/Scripts/MonoSingleton.cs create mode 100644 Assets/Scripts/MonoSingleton.cs.meta create mode 100644 Assets/Scripts/SceneSingleton.meta diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index a95d6cc..038d7f1 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -3,7 +3,7 @@ using UnityEngine; using UnityEngine.SceneManagement; -public class GameManager : Singleton +public class GameManager : MonoSingleton { [SerializeField] diff --git a/Assets/Scripts/MonoSingleton.cs b/Assets/Scripts/MonoSingleton.cs new file mode 100644 index 0000000..4f43af7 --- /dev/null +++ b/Assets/Scripts/MonoSingleton.cs @@ -0,0 +1,63 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public abstract class MonoSingleton : MonoBehaviour where T : Component +{ + + #region Fields + + /// + /// The instance. + /// + private static T instance; + + #endregion + + #region Properties + + /// + /// Gets the instance. + /// + /// The instance. + public static T Instance + { + get + { + if ( instance == null ) + { + instance = FindObjectOfType (); + if ( instance == null ) + { + GameObject obj = new GameObject (); + obj.name = typeof ( T ).Name; + instance = obj.AddComponent (); + } + } + return instance; + } + } + + #endregion + + #region Methods + + /// + /// Use this for initialization. + /// + protected virtual void Awake () + { + if ( instance == null ) + { + instance = this as T; + DontDestroyOnLoad ( gameObject ); + } + else + { + Destroy ( gameObject ); + } + } + + #endregion + +} \ No newline at end of file diff --git a/Assets/Scripts/MonoSingleton.cs.meta b/Assets/Scripts/MonoSingleton.cs.meta new file mode 100644 index 0000000..6caa1e7 --- /dev/null +++ b/Assets/Scripts/MonoSingleton.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: ce49cf29f5faf754289d9c206d19cc66 +timeCreated: 1505546773 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SceneSingleton.meta b/Assets/Scripts/SceneSingleton.meta new file mode 100644 index 0000000..4eaccc1 --- /dev/null +++ b/Assets/Scripts/SceneSingleton.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e908ba46bbf585e47b1ae4539549e29c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Singleton.cs b/Assets/Scripts/Singleton.cs index 13bbc21..5e4af60 100644 --- a/Assets/Scripts/Singleton.cs +++ b/Assets/Scripts/Singleton.cs @@ -1,63 +1,82 @@ -using System.Collections; +using System.Collections; using System.Collections.Generic; using UnityEngine; -public abstract class Singleton : MonoBehaviour where T : Component +public abstract class Singleton where T : Singleton, new() { - - #region Fields - - /// - /// The instance. - /// - private static T instance; - - #endregion - - #region Properties - - /// - /// Gets the instance. - /// - /// The instance. - public static T Instance - { - get - { - if ( instance == null ) - { - instance = FindObjectOfType (); - if ( instance == null ) - { - GameObject obj = new GameObject (); - obj.name = typeof ( T ).Name; - instance = obj.AddComponent (); - } - } - return instance; - } - } - - #endregion - - #region Methods - - /// - /// Use this for initialization. - /// - protected virtual void Awake () - { - if ( instance == null ) - { - instance = this as T; - DontDestroyOnLoad ( gameObject ); - } - else - { - Destroy ( gameObject ); - } - } - - #endregion - + + #region Fields + + /// + /// The instance. + /// + private static T instance; + + #endregion + + #region Properties + + /// + /// Gets the instance. + /// + /// The instance. + public static T Instance + { + get + { + if (Instance == null) + { + //ensure that only one thread can execute + lock (typeof(T)) + { + if (Instance == null) + { + instance = new T(); + instance.Init(); + } + } + } + + return instance; + } + } + + + #endregion + + #region Methods + + public static void CreateInstance() + { + DestroyInstance(); + instance = Instance; + } + + public static void DestroyInstance() + { + if (instance == null) return; + + instance.Clear(); + instance = default(T); + } + + protected void Init() + { + OnInit(); + } + + public void Clear() + { + OnClear(); + } + + protected virtual void OnInit() + { + } + + protected virtual void OnClear() + { + } + #endregion + } \ No newline at end of file diff --git a/Assets/Scripts/Singleton.cs.meta b/Assets/Scripts/Singleton.cs.meta index 6caa1e7..7ef7e61 100644 --- a/Assets/Scripts/Singleton.cs.meta +++ b/Assets/Scripts/Singleton.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 -guid: ce49cf29f5faf754289d9c206d19cc66 -timeCreated: 1505546773 -licenseType: Free +guid: face7a58eceba034f892c28bd5a18238 MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index c0a7b29..d579725 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1 +1,2 @@ -m_EditorVersion: 2017.1.0p4 +m_EditorVersion: 2021.3.6f1c1 +m_EditorVersionWithRevision: 2021.3.6f1c1 (07401303b748)