using System.Collections; using System.Collections.Generic; using UnityEngine; public class Singleton : MonoBehaviour where T : Component { protected static T instance; public static bool HasInstance => instance != null; public static T TryGetInstance() => HasInstance ? instance : null; public static T Current => instance; public static T Instance { get { if (instance == null) { instance = FindFirstObjectByType(); if (instance == null) { GameObject obj = new GameObject(); obj.name = typeof(T).Name + "AutoCreated"; instance = obj.AddComponent(); } } return instance; } } protected virtual void Awake() => InitializeSingleton(); protected virtual void InitializeSingleton() { if (!Application.isPlaying) return; instance = this as T; } }