unity---单例模式


单例模式

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Singleton where T : new ()//泛型约束 必须有无参的构造函数
{
    // Start is called before the first frame update
    // 凡是单例模式类,都可以继承该类,实现单例
    private static T instance ;
    public static T Instance {
        get{
            if(instance==null){
                instance = new T();
                
            }
            return instance;
        }
    }
    // Update is called once per frame
}

用法