Beispiel #1
0
        public override void OnInspectorGUI()
        {
            ++EditorGUI.indentLevel;
            SingletonRoot script = (SingletonRoot)target;

            mConstantRepaint = false;
            for (int i = 0; i < script.singletonInstances.Count; ++i)
            {
                ISingleton inst = script.singletonInstances[i];
                if (inst is MonoBehaviour)
                {
                    continue;
                }

                Type   type    = inst.GetType();
                string name    = inst.GetType().FullName;
                bool   foldout = EditorGUILayout.Foldout(Foldout(name), name);
                if (foldout)
                {
                    SingletonRoot.IEditorDrawer drawer;
                    if (SingletonRoot.editorDrawers.TryGetValue(type, out drawer))
                    {
                        mConstantRepaint |= drawer.constantRepaint;
                        drawer.Draw(inst);
                    }
                }
                Foldout(name, foldout);
            }
            --EditorGUI.indentLevel;
        }
Beispiel #2
0
    void LoadSingletonClasses()
    {
        LoadedData.singletonList = new Dictionary <Type, ISingleton>();

        Type[] types = new Type[0];
        Type   t     = typeof(ISingleton);

        types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => t.IsAssignableFrom(p)).ToArray();


        for (int i = 0; i < types.Length; i++)
        {
            ConstructorInfo info = types[i].GetConstructor(new Type[0]);
            ISingleton      inst = null;

            if (info != null)
            {
                inst = info.Invoke(new object[0]) as ISingleton;

                MonoBehaviour singleton = new GameObject(inst.GetType().FullName).AddComponent(inst.GetType()) as MonoBehaviour;
                DontDestroyOnLoad(singleton.gameObject);

                ISingleton castedSingleton = singleton as ISingleton;
                castedSingleton.RunOnCreated();

                LoadedData.singletonList.Add(types[i], castedSingleton);
            }
        }
    }
Beispiel #3
0
            private void EnforceThisAsSingleton()
            {
                var        targTp = _target.GetType();
                ISingleton c;

                if (!_singletonRefs.TryGetValue(targTp, out c))
                {
                    c = null;
                }

                if (object.ReferenceEquals(c, null) || c.component == null || c.component == _target.component || !c.isActiveAndEnabled)
                {
                    _singletonRefs[targTp] = _target;
                }
                else if (_target.component != null)
                {
                    if (_lifeCycle.HasFlag(SingletonLifeCycleRule.AlwaysReplace))
                    {
                        _singletonRefs[targTp] = _target;
                        if (_target.component.HasComponent <SingletonManager>())
                        {
                            Object.Destroy(c.component);
                        }
                        else
                        {
                            Object.Destroy(c.gameObject);
                        }
                    }
                    else
                    {
                        if (_target.component.HasComponent <SingletonManager>())
                        {
                            Object.Destroy(_target.component);
                        }
                        else
                        {
                            Object.Destroy(_target.gameObject);
                        }
                        throw new System.InvalidOperationException("Attempted to create an instance of a Singleton out of its appropriate operating bounds.");
                    }
                }

                this.UpdateMaintainOnLoadStatus();
            }
Beispiel #4
0
 public static void Remove(ISingleton singleton)
 {
     if (root.singletonInstances.Remove(singleton))
     {
         if (singleton is MonoBehaviour)
         {
             GameObject.Destroy(((MonoBehaviour)singleton).gameObject);
         }
         SingletonLog.InfoFormat("{0} Removed", singleton.GetType().FullName);
     }
 }
        /// <summary>
        /// <para>Registers a singleton to the specified singletonType.</para>
        /// <para>Use this to set a singleton to any type that it derives from</para>
        /// </summary>
        /// <param name="singletonType">The type that will be mapped</param>
        /// <param name="singleton">The singleton being registered</param>
        public static void RegisterSingleton(Type singletonType, ISingleton singleton)
        {
            //The singleton should be of the selected type or it must derive from the selected type
            if (!singletonType.IsAssignableFrom(singletonType))
            {
                BroadcastErrorMessage(CONFLICTING_SINGLETON_TYPE, singleton.GetType(), singletonType);
                return;
            }

            if (_singletons.ContainsKey(singletonType))
            {
                _singletons[singletonType] = singleton;
            }
            else
            {
                _singletons.Add(singletonType, singleton);
            }
        }