Files
5.24BuYu/Assets/Framework/Base/MonoSingleton.cs
T
2025-12-11 16:40:24 +08:00

30 lines
686 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance==null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
_instance = new GameObject().AddComponent<T>();
}
}
return _instance;
}
}
protected virtual void Awake()
{
_instance = this as T;
DontDestroyOnLoad(this.gameObject);
}
}