资源大湿 发表于 2019-9-5 10:54

unity不想创建对象挂脚本实现单例模式

using UnityEngine;

/// <summary>
/// Inherit from this base class to create a singleton.
/// e.g. public class MyClassName : Singleton<MyClassName> {}
/// </summary>
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    // Check to see if we're about to be destroyed.
    private static bool m_ShuttingDown = false;
    private static object m_Lock = new object();
    private static T m_Instance;

    /// <summary>
    /// Access singleton instance through this propriety.
    /// </summary>
    public static T Instance
    {
      get
      {
            if (m_ShuttingDown)
            {
                Debug.LogWarning(" Instance '" + typeof(T) +
                  "' already destroyed. Returning null.");
                return null;
            }

            lock (m_Lock)
            {
                if (m_Instance == null)
                {
                  // Search for existing instance.
                  m_Instance = (T)FindObjectOfType(typeof(T));

                  // Create new instance if one doesn't already exist.
                  if (m_Instance == null)
                  {
                        // Need to create a new GameObject to attach the singleton to.
                        var singletonObject = new GameObject();
                        m_Instance = singletonObject.AddComponent<T>();
                        singletonObject.name = typeof(T).ToString() + " (Singleton)";

                        // Make instance persistent.
                        DontDestroyOnLoad(singletonObject);
                  }
                }

                return m_Instance;
            }
      }
    }


    private void OnApplicationQuit()
    {
      m_ShuttingDown = true;
    }


    private void OnDestroy()
    {
      m_ShuttingDown = true;
    }
}地址:https://wiki.unity3d.com/index.php/Singleton

其他类public class IAPManager : Singleton<IAPManager>, IStoreListener继承此类自动初始化 自动销毁对象,免去策划忘记拉对象的尴尬

资源大湿 发表于 2019-9-5 10:56

using UnityEngine;

/// <summary>
/// Inherit from this base class to create a singleton.
/// e.g. public class MyClassName : Singleton<MyClassName> {}
/// </summary>
///
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    public bool dontDestroy = false;
    private static T m_instance;

    public static T Instance
    {
      get
      {
            if (m_instance==null)
            {
                m_instance = GameObject.FindObjectOfType<T>();
                if (m_instance==null)
                {
                  GameObject singleton = new GameObject(typeof(T).Name);
                  m_instance = singleton.AddComponent<T>();
                }
            }

            return m_instance;
      }
    }

    public virtual void Awake()
    {
      if (m_instance==null)
      {
            m_instance = this as T;
            if (dontDestroy)
            {
                transform.parent = null;
                DontDestroyOnLoad(this.gameObject);
            }
      }
      else
      {
            Destroy(gameObject);
      }
    }

}
页: [1]
查看完整版本: unity不想创建对象挂脚本实现单例模式