单例模式
优点:1.同时间只存在一个对象。
2.快速获取对象的方法。
3.适合游戏中单一功能的管理器。
//第一种方式
public static Player instance;
//第二种方式
private static Player instance;
public static Player Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<Player>();
}
return instance;
}
}
//第三种方式 (单例模式模板类)
using UnityEngine;
public class UnitySingleton<T> : MonoBehaviour
where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_inst