diff --git a/Runtime/Patterns/Singleton/MonoSingleton.cs b/Runtime/Patterns/Singleton/MonoSingleton.cs
index 315a302..04bca41 100644
--- a/Runtime/Patterns/Singleton/MonoSingleton.cs
+++ b/Runtime/Patterns/Singleton/MonoSingleton.cs
@@ -2,15 +2,17 @@
namespace StansAssets.Foundation.Patterns
{
- ///
+ ///
/// Singleton pattern implementation.
/// Can be used with classes extended from a MonoBehaviour.
/// Once instance is found or created, game object will be marked as DontDestroyOnLoadю
///
public abstract class MonoSingleton : MonoBehaviour where T : MonoBehaviour
- {
- static T s_Instance;
- static bool s_ApplicationIsQuitting;
+ {
+ static T s_Instance;
+ static bool s_ApplicationIsQuitting;
+ static bool s_IsDestroyed;
+
protected virtual void Awake()
{
@@ -25,11 +27,13 @@ protected virtual void Awake()
///
public static T Instance
{
- get
- {
- if (s_ApplicationIsQuitting)
- {
- Debug.LogError($"{typeof(T)} [MonoSingleton] is already destroyed. Please check HasInstance before accessing instance in the destructor.");
+ get
+ {
+ if (s_ApplicationIsQuitting)
+ {
+ Debug.LogError(
+ $"{typeof(T)} [MonoSingleton] is already destroyed. " +
+ $"Please check {nameof(HasInstance)} or {nameof(IsDestroyed)} before accessing instance in the destructor.");
return null;
}
@@ -39,9 +43,10 @@ public static T Instance
if (s_Instance == null)
Instantiate();
}
- return s_Instance;
- }
- }
+
+ return s_Instance;
+ }
+ }
///
/// Methods will create new object Instantiate
@@ -57,33 +62,36 @@ public static void Instantiate()
}
///
- /// True if Singleton Instance exists
+ /// Returns `true` if Singleton Instance exists.
///
- public static bool HasInstance => !IsDestroyed;
+ public static bool HasInstance => s_Instance != null;
///
- /// True if Singleton Instance doesn't exist
+ /// If this property returns `true` it means that object with explicitly destroyed.
+ /// This could happen if Destroy function was called for this object or if it was
+ /// automatically destroyed during the `ApplicationQuit`.
///
- public static bool IsDestroyed => s_Instance == null;
+ public static bool IsDestroyed => s_IsDestroyed;
///
- /// When Unity quits, it destroys objects in a random order.
- /// In principle, a Singleton is only destroyed when application quits.
- /// If any script calls Instance after it have been destroyed,
- /// it will create a buggy ghost object that will stay on the Editor scene
- /// even after stopping playing the Application. Really bad!
- /// So, this was made to be sure we're not creating that buggy ghost object.
- ///
- protected virtual void OnDestroy ()
+ /// When Unity quits, it destroys objects in a random order.
+ /// In principle, a Singleton is only destroyed when application quits.
+ /// If any script calls Instance after it have been destroyed,
+ /// it will create a buggy ghost object that will stay on the Editor scene
+ /// even after stopping playing the Application. Really bad!
+ /// So, this was made to be sure we're not creating that buggy ghost object.
+ ///
+ protected virtual void OnDestroy()
{
- s_Instance = null;
- s_ApplicationIsQuitting = true;
- }
+ s_Instance = null;
+ s_IsDestroyed = true;
+ }
- protected virtual void OnApplicationQuit ()
- {
- s_Instance = null;
- s_ApplicationIsQuitting = true;
- }
- }
-}
+ protected virtual void OnApplicationQuit()
+ {
+ s_Instance = null;
+ s_IsDestroyed = true;
+ s_ApplicationIsQuitting = true;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Runtime/Patterns/Singleton/Singleton.cs b/Runtime/Patterns/Singleton/Singleton.cs
index 66add08..3096f90 100644
--- a/Runtime/Patterns/Singleton/Singleton.cs
+++ b/Runtime/Patterns/Singleton/Singleton.cs
@@ -1,15 +1,15 @@
namespace StansAssets.Foundation.Patterns
{
- ///
- /// Singleton pattern implementation.
- ///
- public abstract class Singleton where T : Singleton, new()
- {
- static T s_Instance;
+ ///
+ /// Singleton pattern implementation.
+ ///
+ public abstract class Singleton where T : Singleton, new()
+ {
+ static T s_Instance;
- ///
- /// Returns a singleton class instance.
- ///
- public static T Instance => s_Instance ?? (s_Instance = new T());
- }
-}
+ ///
+ /// Returns a singleton class instance.
+ ///
+ public static T Instance => s_Instance ?? (s_Instance = new T());
+ }
+}
\ No newline at end of file