public abstract class MonoSingleton<T> : MonoBehaviour where T : Component
{
private static object _lock = new object();
private static bool _shuttingDown = false;
private static T _instance;
public static T Instance
{
get
{
if(_shuttingDown)
{
Debug.Log($"[Singleton] Instance '{typeof(T)} already destroyed. returning null'");
return null;
}
lock(_lock)
{
if(_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if(_instance == null)
{
GameObject newGameObject = new GameObject(typeof(T).Name, typeof(T));
_instance = newGameObject.GetComponent<T>();
}
}
}
return _instance;
}
}
protected virtual void Awake()
{
lock(_lock)
{
var objects = FindObjectsOfType(GetType());
if(objects.Length > 1 && _instance != null && _instance != this)
{
Destroy(this.gameObject);
return;
}
SetDontDestroy(this.transform);
}
}
private static void SetDontDestroy(Transform transform)
{
if(Application.isPlaying == true)
{
DontDestroyOnLoad(transform.root.gameObject);
}
else
{
DontDestroyOnLoad(transform.gameObject);
}
}
private void OnApplicationQuit()
{
if(_instance)
{
_shuttingDown = true;
}
}
private void OnDestroy()
{
if(_instance && _instance == this)
{
_shuttingDown = true;
}
}
}
어디서병신같은코드줏어왔네ㅉㅉ
너는 코드공유도못하는 병신보다 못한 새끼이면서 왜이리 주접싸누
저거실드치면 너도심각한수준인거야 모르면틀린답말해주는게아니라 닥치고있어야된다