using UnityEngine;
public class MonoSigleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
var objects = GameObject.FindObjectsOfType<T>();
if (objects.Length > 0)
{
if (objects.Length > 1)
{
for (int i = 1; i < objects.Length; i++)
{
Destroy(objects[i]);
}
}
_instance = objects[0];
}
}
if (_instance == null && Application.isPlaying)
{
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
}
return _instance;
}
}
}