单利

发布时间 2023-03-22 21:11:23作者: 天分丶张三

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;

}
}

}