56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace Gaia
|
|
{
|
|
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
private static T _instance;
|
|
|
|
private static object _lock = new object();
|
|
|
|
private static bool applicationIsQuitting = false;
|
|
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (applicationIsQuitting)
|
|
{
|
|
Debug.LogWarning("[Singleton] Instance '" + typeof(T)?.ToString() + "' already destroyed on application quit. Won't create again - returning null.");
|
|
return null;
|
|
}
|
|
lock (_lock)
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = (T)Object.FindObjectOfType(typeof(T));
|
|
if (Object.FindObjectsOfType(typeof(T)).Length > 1)
|
|
{
|
|
Debug.LogError("[Singleton] Something went really wrong - there should never be more than 1 singleton! Reopenning the scene might fix it.");
|
|
return _instance;
|
|
}
|
|
if (_instance == null)
|
|
{
|
|
GameObject gameObject = new GameObject();
|
|
_instance = gameObject.AddComponent<T>();
|
|
gameObject.name = "(singleton) " + typeof(T).ToString();
|
|
Object.DontDestroyOnLoad(gameObject);
|
|
Debug.Log("[Singleton] An instance of " + typeof(T)?.ToString() + " is needed in the scene, so '" + gameObject?.ToString() + "' was created with DontDestroyOnLoad.");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("[Singleton] Using instance already created: " + _instance.gameObject.name);
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
applicationIsQuitting = true;
|
|
}
|
|
}
|
|
}
|