C# 单例

  using UnityEngine;
  using System.Collections;

  public class Singleton : MonoBehaviour where T : Component
  {
      private static readonly object syslock = new object();  //我叫他线程锁
      private static T _instance;
      public static T Instance
      {
          get
          {
              if (_instance == null)
              {
                  lock (syslock)
                  { //锁一下,避免多线程出问题
                      _instance = FindObjectOfType(typeof(T)) as T;
                      if (_instance == null)
                      {
                          GameObject obj = new GameObject(typeof(T).Name);
                          //obj.hideFlags = HideFlags.DontSave;
                          //obj.hideFlags = HideFlags.HideAndDontSave;
                          _instance = (T)obj.AddComponent(typeof(T));
                }
            }
        }
        return _instance;
    }
}
//  这是为了不要在切换场景时单例消失,可以删掉
public virtual void Awake()
{
    DontDestroyOnLoad(this.gameObject);
    if (_instance == null)
    {
        _instance = this as T;
    }
    else
    {
        Destroy(gameObject);
    }
}
public static bool IsBuild
{
    get
    {
        return _instance != null;
    }
}

      // Use this for initialization
      void Start()
      {

      }

      // Update is called once per frame
      void Update()
      {

      }
  }
  public class Button_config : Singleton
  {
  }

你可能感兴趣的:(C# 单例)