Unity - 单例模式实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
public static T Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = (T) this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}