Files
2026-03-04 10:03:45 +08:00

34 lines
630 B
C#

using UnityEngine;
namespace CTS
{
public class CTSSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
GameObject gameObject = new GameObject();
_instance = gameObject.AddComponent<T>();
gameObject.name = typeof(T).ToString();
gameObject.hideFlags = HideFlags.HideAndDontSave;
if (Application.isPlaying)
{
Object.DontDestroyOnLoad(gameObject);
}
}
return _instance;
}
}
}
}
}