Пример #1
0
        /// <summary>
        /// 注册一个单例
        /// </summary>
        /// <param name="_singleton">要注册的单例</param>
        public static void Regist(SingletonBase _singleton)
        {
            //Debug.Log(_singleton.GetType().ToString());
#if UNITY_EDITOR
            if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
            {
                Debug.LogError("正在关闭,但是却创建了单例");
            }
#endif
            if (allSingletons == null)
            {
                managerObj = new GameObject("[Singleton Manager]");
                managerObj.AddComponent <SingletonManager>();
                DontDestroyOnLoad(managerObj);
                managerObj.hideFlags = shouldThisObjectHide ? (HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor) : HideFlags.None;
                allSingletons        = new HashSet <SingletonBase>();
            }

            if (allSingletons.Contains(_singleton))
            {
                throw new System.Exception(string.Format("重复注册单例 : {0}", _singleton.GetType().FullName));
            }

            allSingletons.Add(_singleton);
            allSingletonsArray = allSingletons.ToArray();
        }
Пример #2
0
        /// <summary>
        /// 注销一个单例
        /// </summary>
        /// <param name="_singleton">要注销的单例</param>
        public static void UnRegist(SingletonBase _singleton, bool _callReleaseMemory = true)
        {
            if (_singleton == null)
            {
                return;
            }

            if (allSingletons == null)
            {
                throw new System.Exception(string.Format("单例管理器SingletonManager尚未初始化,不允许注销单例 : {0}", _singleton.GetType().FullName));
            }

            if (!allSingletons.Contains(_singleton))
            {
                throw new System.Exception(string.Format("单例{0}没有注册过,不允许注销", _singleton.GetType().FullName));
            }

            if (_callReleaseMemory)
            {
                _singleton.ReleaseMemories();
            }

            _singleton.Unload();
            _singleton.Loaded = false;
            allSingletons.Remove(_singleton);
            allSingletonsArray = allSingletons.ToArray();

            ((System.IDisposable)_singleton).Dispose();
        }
Пример #3
0
        /// <summary>
        /// 注册一个单例
        /// </summary>
        /// <param name="_singleton">要注册的单例</param>
        public static void Regist(SingletonBase _singleton)
        {
            //Debug.Log(_singleton.GetType().ToString());

            if (allSingletons == null)
            {
                managerObj = new GameObject("[Singleton Manager]");
                managerObj.AddComponent <SingletonManager>();
                DontDestroyOnLoad(managerObj);
                managerObj.hideFlags = shouldThisObjectHide ? HideFlags.HideAndDontSave : HideFlags.None;
                allSingletons        = new HashSet <SingletonBase>();
            }
            if (allSingletons.Contains(_singleton))
            {
                throw new System.Exception(string.Format("重复注册单例 : {0}", _singleton.GetType().FullName));
            }

            allSingletons.Add(_singleton);
            allSingletonsArray = allSingletons.ToArray();
        }