34 lines
630 B
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|