From bd73ecfa82a78273f754066d0fd9753f42b97a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Constant?= Date: Tue, 18 Jun 2024 10:19:24 +0200 Subject: [PATCH 1/4] Remove region comments --- Runtime/Singleton.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Runtime/Singleton.cs b/Runtime/Singleton.cs index 9bd45b6..7576478 100644 --- a/Runtime/Singleton.cs +++ b/Runtime/Singleton.cs @@ -5,14 +5,10 @@ namespace FishingCactus.CommonCode public class Singleton<_INSTANCE_> : MonoBehaviour where _INSTANCE_ : Singleton<_INSTANCE_> { - // -- FIELDS - [SerializeField] private bool DestroyOnLoad = false; private static _INSTANCE_ _instance = null; - // -- PROPERTIES - public static _INSTANCE_ Instance { get @@ -28,8 +24,6 @@ public static _INSTANCE_ Instance public static bool HasInstance => _instance != null; - // -- UNITY - public virtual void Awake() { if( !DestroyOnLoad ) From 08325ae16fa1933af03e7407c5ee196ab1815001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Constant?= Date: Tue, 18 Jun 2024 10:20:14 +0200 Subject: [PATCH 2/4] Put static field above --- Runtime/Singleton.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/Singleton.cs b/Runtime/Singleton.cs index 7576478..0154361 100644 --- a/Runtime/Singleton.cs +++ b/Runtime/Singleton.cs @@ -5,10 +5,10 @@ namespace FishingCactus.CommonCode public class Singleton<_INSTANCE_> : MonoBehaviour where _INSTANCE_ : Singleton<_INSTANCE_> { - [SerializeField] private bool DestroyOnLoad = false; - private static _INSTANCE_ _instance = null; + [SerializeField] private bool DestroyOnLoad = false; + public static _INSTANCE_ Instance { get From 796dc89d565df996a6b3ae2f3c4377b3ef5b33ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Constant?= Date: Tue, 18 Jun 2024 11:40:19 +0200 Subject: [PATCH 3/4] Add Singleton.TryGetInstance --- Runtime/Singleton.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Runtime/Singleton.cs b/Runtime/Singleton.cs index 0154361..b4fda9c 100644 --- a/Runtime/Singleton.cs +++ b/Runtime/Singleton.cs @@ -24,6 +24,13 @@ public static _INSTANCE_ Instance public static bool HasInstance => _instance != null; + public static bool TryGetInstance( out _INSTANCE_ instance ) + { + instance = Instance; + + return instance != null; + } + public virtual void Awake() { if( !DestroyOnLoad ) @@ -43,4 +50,4 @@ public virtual void Awake() } } } -} \ No newline at end of file +} From 13374996245554742b4d299461c1baa798eead04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Constant?= Date: Tue, 18 Jun 2024 11:42:18 +0200 Subject: [PATCH 4/4] Improve log when 2 singletons detected + make it a warning --- Runtime/Singleton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Singleton.cs b/Runtime/Singleton.cs index b4fda9c..101362e 100644 --- a/Runtime/Singleton.cs +++ b/Runtime/Singleton.cs @@ -44,7 +44,7 @@ public virtual void Awake() } else if( _instance != this ) { - Debug.LogError( $"{_instance.name} is added two times.", this ); + Debug.LogWarning( $"There are two instances of {_instance.name}. The latest one will be destroyed.", _instance ); Destroy( this ); }