From 76908dbf8a8d50cec2c3f67f23f8d6322847ac1e Mon Sep 17 00:00:00 2001 From: CaicukunChiji Date: Wed, 9 Jul 2025 16:45:29 +0800 Subject: [PATCH] (client) feat:Add Singleton and MonoSingleton class --- Client/Assets/Scripts/Utils.meta | 3 ++ Client/Assets/Scripts/Utils/MonoSingleton.cs | 32 +++++++++++++++++++ .../Scripts/Utils/MonoSingleton.cs.meta | 2 ++ Client/Assets/Scripts/Utils/Singleton.cs | 8 +++++ Client/Assets/Scripts/Utils/Singleton.cs.meta | 2 ++ 5 files changed, 47 insertions(+) create mode 100644 Client/Assets/Scripts/Utils.meta create mode 100644 Client/Assets/Scripts/Utils/MonoSingleton.cs create mode 100644 Client/Assets/Scripts/Utils/MonoSingleton.cs.meta create mode 100644 Client/Assets/Scripts/Utils/Singleton.cs create mode 100644 Client/Assets/Scripts/Utils/Singleton.cs.meta diff --git a/Client/Assets/Scripts/Utils.meta b/Client/Assets/Scripts/Utils.meta new file mode 100644 index 0000000..bea004f --- /dev/null +++ b/Client/Assets/Scripts/Utils.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 44fba140ed5941d3999e1ff66685b176 +timeCreated: 1752049808 \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/MonoSingleton.cs b/Client/Assets/Scripts/Utils/MonoSingleton.cs new file mode 100644 index 0000000..dfb1e55 --- /dev/null +++ b/Client/Assets/Scripts/Utils/MonoSingleton.cs @@ -0,0 +1,32 @@ +using UnityEngine; + +namespace Utils +{ + public abstract class MonoSingleton : MonoBehaviour where T : MonoBehaviour + { + private static T _instance; + public bool isGlobal = true; + + public static T Instance => _instance ??= FindFirstObjectByType(); + + private void Awake() + { + Debug.LogWarning($"{typeof(T)} [{GetInstanceID()}] Awake"); + if (isGlobal) + { + if (_instance is not null && _instance != gameObject.GetComponent()) + { + Destroy(gameObject); + return; + } + + DontDestroyOnLoad(gameObject); + _instance = gameObject.GetComponent(); + } + + OnStart(); + } + + protected abstract void OnStart(); + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/MonoSingleton.cs.meta b/Client/Assets/Scripts/Utils/MonoSingleton.cs.meta new file mode 100644 index 0000000..c8cd51d --- /dev/null +++ b/Client/Assets/Scripts/Utils/MonoSingleton.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9abe30aefbadea24b9a23da5020a413c \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/Singleton.cs b/Client/Assets/Scripts/Utils/Singleton.cs new file mode 100644 index 0000000..cb034c0 --- /dev/null +++ b/Client/Assets/Scripts/Utils/Singleton.cs @@ -0,0 +1,8 @@ +namespace Utils +{ + public class Singleton where T : new() + { + private static T _instance; + public static T Instance => _instance ??= new T(); + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/Singleton.cs.meta b/Client/Assets/Scripts/Utils/Singleton.cs.meta new file mode 100644 index 0000000..b4cddf5 --- /dev/null +++ b/Client/Assets/Scripts/Utils/Singleton.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0937f7a41cd612b4aae1f33f9c1d938a \ No newline at end of file