单利

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 泛型单利
/// </summary>
/// <typeparam name="T"></typeparam>
/// 限制:类 ,无参
public class Singleton<T> where T : class, new()
{
private static T ins;
private static readonly object obj = new object();
public static T Instance
{
get
{
//如果传入的变量是空 否则返回此对象
if (ins == null)
{
//锁
lock (obj)
{
if (ins == null)
{
//就new一个
ins = new T();
}
}


}
//返回
return ins;

}
}

}

posted @ 2023-03-21 14:25  天分丶张三  阅读(16)  评论(0编辑  收藏  举报