hellbound/Assets/Sources/Feel/MMTools/Tools/MMSingletons/MMPersistentSingleton.cs

76 lines
1.8 KiB
C#
Raw Permalink Normal View History

2021-11-26 11:16:25 +03:00
using UnityEngine;
namespace MoreMountains.Tools
{
/// <summary>
/// Persistent singleton.
/// </summary>
public class MMPersistentSingleton<T> : MonoBehaviour where T : Component
{
[Header("Persistent Singleton")]
/// if this is true, this singleton will auto detach if it finds itself parented on awake
[Tooltip("if this is true, this singleton will auto detach if it finds itself parented on awake")]
public bool AutomaticallyUnparentOnAwake = true;
public static bool HasInstance => _instance != null;
public static T Current => _instance;
protected static T _instance;
protected bool _enabled;
/// <summary>
/// Singleton design pattern
/// </summary>
/// <value>The instance.</value>
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T> ();
if (_instance == null)
{
GameObject obj = new GameObject ();
_instance = obj.AddComponent<T> ();
}
}
return _instance;
}
}
/// <summary>
/// On awake, we check if there's already a copy of the object in the scene. If there's one, we destroy it.
/// </summary>
protected virtual void Awake ()
{
if (!Application.isPlaying)
{
return;
}
if (AutomaticallyUnparentOnAwake)
{
this.transform.SetParent(null);
}
if (_instance == null)
{
//If I am the first instance, make me the Singleton
_instance = this as T;
DontDestroyOnLoad (transform.gameObject);
_enabled = true;
}
else
{
//If a Singleton already exists and you find
//another reference in scene, destroy it!
if(this != _instance)
{
Destroy(this.gameObject);
}
}
}
}
}