97 lines
3.5 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using System;
using UnityEngine;
2024-10-17 01:59:25 +08:00
namespace JNGame.Runtime.Util
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
///
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
public class SingletonScene<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
private static object _lock = new object();
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
public static T Instance
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
get
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
if (applicationIsQuitting)
{
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
"' already destroyed on application quit." +
" Won't create again - returning null.");
return null;
}
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
lock (_lock)
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
if (_instance == null)
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
try
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
_instance = (T)FindObjectOfType(typeof(T));
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
if (FindObjectsOfType(typeof(T)).Length > 1)
{
Debug.LogError("[Singleton] Something went really wrong " +
" - there should never be more than 1 singleton!" +
" Reopening the scene might fix it.");
return _instance;
}
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton) " + typeof(T).ToString();
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
DontDestroyOnLoad(singleton);
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"' was created with DontDestroyOnLoad.");
}
else
{
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
2024-08-17 14:27:18 +08:00
}
2024-10-17 01:59:25 +08:00
catch (Exception e)
2024-08-17 14:27:18 +08:00
{
2024-10-17 01:59:25 +08:00
Debug.LogError(e.Message);
2024-08-17 14:27:18 +08:00
}
}
2024-10-17 01:59:25 +08:00
return _instance;
}
2024-08-17 14:27:18 +08:00
}
}
2024-10-17 01:59:25 +08:00
private static bool applicationIsQuitting = false;
2024-08-17 14:27:18 +08:00
2024-10-17 01:59:25 +08:00
/// <summary>
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
public void OnDestroy()
{
applicationIsQuitting = true;
OnDispose();
}
protected virtual void OnDispose()
{
}
2024-08-17 14:27:18 +08:00
}
}