Пример #1
0
 static void CreateInstance()
 {
     // If scripts are recompiled the the static variable will be lost.
     // Some users recompile scripts in play mode and then reload the scene (https://forum.arongranberg.com/t/rts-game-pathfinding/6623/48?u=aron_granberg)
     // which makes this a requirement
     instance = FindObjectOfType <BatchedEvents>();
     if (instance == null)
     {
         var go = new UnityEngine.GameObject("Batch Helper");
         instance     = go.AddComponent <BatchedEvents>();
         go.hideFlags = UnityEngine.HideFlags.HideAndDontSave;
         DontDestroyOnLoad(go);
     }
 }
Пример #2
0
        void OnEnable()
        {
            if (instance == null)
            {
                instance = this;
            }
            if (instance != this)
            {
                // We cannot destroy the object while it is being enabled, so we need to delay it a bit
#if UNITY_EDITOR
                // This is only important in the editor to avoid a build-up of old managers.
                // In an actual game at most 1 (though in practice zero) old managers will be laying around.
                // It would be nice to use a coroutine for this instead, but unfortunately they do not work for objects marked with HideAndDontSave.
                UnityEditor.EditorApplication.update += DelayedDestroy;
#endif
                return;
            }
        }
Пример #3
0
        static void CreateInstance()
        {
            // If scripts are recompiled the the static variable will be lost.
            // Some users recompile scripts in play mode and then reload the scene (https://forum.arongranberg.com/t/rts-game-pathfinding/6623/48?u=aron_granberg)
            // which makes handling this a requirement.

            // Here one might try to look for existing instances of the class that haven't yet been enabled.
            // However, this turns out to be tricky.
            // Resources.FindObjectsOfTypeAll<T>() is the only call that includes HideInInspector GameObjects.
            // But it is hard to distinguish between objects that are internal ones which will never be enabled and objects that will be enabled.
            // Checking .gameObject.scene.isLoaded doesn't work reliably (object may be enabled and working even if isLoaded is false)
            // Checking .gameObject.scene.isValid doesn't work reliably (object may be enabled and working even if isValid is false)

            // So instead we just always create a new instance. This is not a particularly heavy operation and it only happens once per game, so why not.
            // The OnEnable call will clean up duplicate managers if there are any.

            var go = new GameObject("Batch Helper")
            {
                hideFlags = HideFlags.DontSave | HideFlags.NotEditable | HideFlags.HideInInspector | HideFlags.HideInHierarchy
            };

            instance = go.AddComponent <BatchedEvents>();
            DontDestroyOnLoad(go);
        }