加载中...

unity---单例模式

单例模式

代码

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

public class Singleton<T> 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
}

用法

image

posted @ 2022-04-16 21:23  lxp_blog  阅读(33)  评论(0)    收藏  举报