using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 通用单例模式
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T instance;
public static T Instance { get => instance; }
public static bool IsInitialized { get => instance != null; }
protected virtual void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = (T)this;
}
}
protected virtual void OnDestroy()
{
if (instance == this)
{
instance = null;
}
}
}
通用单例模式
于 2022-07-19 18:00:46 首次发布