SamsonGame/Assets/Scripts/Core/Utils/Singleton.cs

30 lines
622 B
C#
Raw Permalink Normal View History

2021-12-29 20:50:11 +03:00
using UnityEngine;
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance = null;
public static T Instance
{
get
{
if (_instance != null)
return _instance;
var fundedInstance = (T)FindObjectOfType(typeof(T));
if (fundedInstance)
{
_instance = fundedInstance;
DontDestroyOnLoad(_instance.gameObject);
}
else
{
_instance = null;
}
return _instance;
}
}
}