using UnityEngine; namespace IcecreamView { /// /// 单例模板类 /// /// 必须为继承MonoBehaviour对象 public class SingletonTemplate : 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(); if (instance == null) { GameObject go = new GameObject(); go.name = typeof(T).Name; instance = go.AddComponent(); } } } return instance; } } } }