Files
taptap2024_GJ_chidouren/Packages/CC-Framework/com.foldcc.icecreamview/Runtime/SingletonTemplate.cs
2024-10-16 00:03:41 +08:00

34 lines
987 B
C#

using UnityEngine;
namespace IcecreamView
{
/// <summary>
/// 单例模板类
/// </summary>
/// <typeparam name="T">必须为继承MonoBehaviour对象</typeparam>
public class SingletonTemplate<T> : MonoBehaviour where T : MonoBehaviour
{
private static volatile T instance;
private static object syncRoot = new Object();
public static T Instance
{
get
{
lock (syncRoot)
{
if (instance == null)
{
instance = FindObjectOfType<T>();
if (instance == null)
{
GameObject go = new GameObject();
go.name = typeof(T).Name;
instance = go.AddComponent<T>();
}
}
}
return instance;
}
}
}
}