using UnityEngine;
namespace FancyUnity
{
///
/// Singleton pattern.
///
public class Singleton : MonoBehaviour where T : Component
{
public bool Perpetual = false;
protected static T _inst;
///
/// Singleton design pattern
///
/// The instance.
public static T Inst
{
get
{
if (_inst == null)
{
GameObject obj = new GameObject(typeof(T).Name);
DontDestroyOnLoad(obj);
_inst = obj.AddComponent();
}
return _inst;
}
}
///
/// On awake, we initialize our instance. Make sure to call base.Awake() in override if you need awake.
///
protected virtual void Awake()
{
if (!Application.isPlaying)
{
return;
}
_inst = this as T;
if (Perpetual)
{
DontDestroyOnLoad(gameObject);
}
}
public virtual void RemoveSelf()
{
_inst = null;
DestroyImmediate(gameObject);
}
}
}