You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

43 lines
952 B

using UnityEngine;
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance = null;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(T)) as T;
if (_instance == null)
{
GameObject obj = new GameObject(nameof(T));
_instance = obj.AddComponent<T>();
}
DontDestroyOnLoad(_instance.gameObject);
}
return _instance;
}
}
public static bool IsNull()
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(T)) as T;
}
return _instance == null;
}
public static void RemoveInstance()
{
if (_instance == null)
return;
_instance = null;
}
}