You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Feel/MMTools/Tools/MMSingletons/MMPersistentSingleton.cs

84 lines
1.9 KiB
C#

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 ();
obj.name = typeof(T).Name + "_AutoCreated";
_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 ()
{
InitializeSingleton();
}
/// <summary>
/// Initializes the singleton.
/// </summary>
protected virtual void InitializeSingleton()
{
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);
}
}
}
}
}