Files
Simulation/Assets/Scripts/Common/SingletonBase.cs

102 lines
2.4 KiB
C#
Raw Normal View History

2025-04-29 10:50:48 +09:00
using UnityEngine;
using System.Collections;
2025-04-30 17:55:20 +09:00
public abstract class UnitySingleton<T> : UnityEngine.MonoBehaviour where T : UnitySingleton<T>
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
private static T instance;
private static object _lock = new object();
protected static bool applicationIsQuitting = false;
public static bool IsApplicationQuitting
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
get
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
return applicationIsQuitting;
2025-04-29 10:50:48 +09:00
}
2025-04-30 17:55:20 +09:00
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
public static T I
{
get
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
if (applicationIsQuitting)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
"' already destroyed on application quit." +
" Won't create again - returning null.");
return null;
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
lock (_lock)
{
if (instance == null)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
instance = FindFirstObjectByType(typeof(T)) as T;
2025-04-29 10:50:48 +09:00
if (instance == null)
{
2025-04-30 17:55:20 +09:00
GameObject singleton = new GameObject(typeof(T).Name);
instance = singleton.AddComponent<T>();
instance.Init();
instance.name = typeof(T).ToString();
DontDestroyOnLoad(instance.gameObject);
2025-04-29 10:50:48 +09:00
}
}
2025-04-30 17:55:20 +09:00
return instance;
2025-04-29 10:50:48 +09:00
}
}
2025-04-30 17:55:20 +09:00
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
void Awake()
{
lock (_lock)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
if (instance == null)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
instance = this as T;
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
}
else if (instance != this)
{
Debug.LogError(this.name + " Destroy !!!");
Destroy(this.gameObject);
return;
2025-04-29 10:50:48 +09:00
}
}
2025-04-30 17:55:20 +09:00
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
protected virtual void Init()
{
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
protected virtual void OnApplicationQuit()
{
2025-04-29 10:50:48 +09:00
#if UNITY_EDITOR
#else
applicationIsQuitting = true;
#endif
}
2025-04-30 17:55:20 +09:00
}
2025-04-29 10:50:48 +09:00
2025-04-30 17:55:20 +09:00
public abstract class ISingleton<T> where T : class, new()
{
static volatile T instance = default(T);
static readonly object padlock = new object();
protected ISingleton()
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
}
public static T I
{
get
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
lock (padlock)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
if (instance == null)
2025-04-29 10:50:48 +09:00
{
2025-04-30 17:55:20 +09:00
instance = new T();
(instance as ISingleton<T>).Init();
2025-04-29 10:50:48 +09:00
}
2025-04-30 17:55:20 +09:00
return instance;
2025-04-29 10:50:48 +09:00
}
}
}
2025-04-30 17:55:20 +09:00
public virtual void Init() { }
2025-04-29 10:50:48 +09:00
}