Unity - 單例模式實(shí)現(xiàn)
2022-07-21 20:54 作者:塵風(fēng)一枚 | 我要投稿

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);
}
}
}
標(biāo)簽: