-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.cs
42 lines (40 loc) · 1.47 KB
/
Singleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 非继承自MonoBehaviour的单例对象继承此类,比如资源管理器等等
/// </summary>
public class Singleton<T> where T : class
{
private static T _instance;
private static readonly object syslock = new object();
//tips:这个是为了作为参数传入函数中,当并不想利用其储存什么数据时,使其长度为0就行。
public static readonly Type[] emptyTypes = new Type[0];
public static T Instance
{
get
{
if (_instance == null)
{
//保证创建的过程是单位操作
lock (syslock)
{
if (_instance == null)
{
//ci的作用是为了确定构造函数是否为private
ConstructorInfo ci = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, emptyTypes, null);
if (ci == null)
{
throw new InvalidOperationException("class must contain a private constructor");
}
//ci为构造函数信息,用ci可以调用构造函数信息
_instance = (T)ci.Invoke(null);
}
}
}
return _instance;
}
}
}