Files
2026-07-21 08:56:10 +03:00

43 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : 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<T>();
if (instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name + "AutoCreated";
instance = obj.AddComponent<T>();
}
}
return instance;
}
}
protected virtual void Awake() => InitializeSingleton();
protected virtual void InitializeSingleton()
{
if (!Application.isPlaying)
return;
instance = this as T;
}
}