找回密码
 立即注册
查看: 877|回复: 1

[简易教程] unity不想创建对象挂脚本实现单例模式

[复制链接]
发表于 2019-9-5 10:54 | 显示全部楼层 |阅读模式
  1. using UnityEngine;

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

  12.     /// <summary>
  13.     /// Access singleton instance through this propriety.
  14.     /// </summary>
  15.     public static T Instance
  16.     {
  17.         get
  18.         {
  19.             if (m_ShuttingDown)
  20.             {
  21.                 Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
  22.                     "' already destroyed. Returning null.");
  23.                 return null;
  24.             }

  25.             lock (m_Lock)
  26.             {
  27.                 if (m_Instance == null)
  28.                 {
  29.                     // Search for existing instance.
  30.                     m_Instance = (T)FindObjectOfType(typeof(T));

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

  38.                         // Make instance persistent.
  39.                         DontDestroyOnLoad(singletonObject);
  40.                     }
  41.                 }

  42.                 return m_Instance;
  43.             }
  44.         }
  45.     }


  46.     private void OnApplicationQuit()
  47.     {
  48.         m_ShuttingDown = true;
  49.     }


  50.     private void OnDestroy()
  51.     {
  52.         m_ShuttingDown = true;
  53.     }
  54. }
复制代码
地址:https://wiki.unity3d.com/index.php/Singleton

其他类public class IAPManager : Singleton<IAPManager>, IStoreListener  继承此类自动初始化 自动销毁对象,免去策划忘记拉对象的尴尬
 楼主| 发表于 2019-9-5 10:56 | 显示全部楼层
  1. using UnityEngine;

  2. /// <summary>
  3. /// Inherit from this base class to create a singleton.
  4. /// e.g. public class MyClassName : Singleton<MyClassName> {}
  5. /// </summary>
  6. ///
  7. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
  8. {
  9.     public bool dontDestroy = false;
  10.     private static T m_instance;

  11.     public static T Instance
  12.     {
  13.         get
  14.         {
  15.             if (m_instance==null)
  16.             {
  17.                 m_instance = GameObject.FindObjectOfType<T>();
  18.                 if (m_instance==null)
  19.                 {
  20.                     GameObject singleton = new GameObject(typeof(T).Name);
  21.                     m_instance = singleton.AddComponent<T>();
  22.                 }
  23.             }

  24.             return m_instance;
  25.         }
  26.     }

  27.     public virtual void Awake()
  28.     {
  29.         if (m_instance==null)
  30.         {
  31.             m_instance = this as T;
  32.             if (dontDestroy)
  33.             {
  34.                 transform.parent = null;
  35.                 DontDestroyOnLoad(this.gameObject);
  36.             }
  37.         }
  38.         else
  39.         {
  40.             Destroy(gameObject);
  41.         }
  42.     }

  43. }
复制代码
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-20 11:00 , Processed in 0.472029 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表