Exemple #1
0
        protected IEnumerator LoadAdditiveSceneAsync(SceneModel desired, float progressModifier = 1f)
        {
            AsyncOperation loadOp = SceneManager.LoadSceneAsync(desired.SceneName, LoadSceneMode.Additive);

            if (loadOp == null)
            {
                Debug.LogError("Failed to load scene: " + desired.SceneName);
                yield break;
            }

            float previousProgress = 0f;

            while (!loadOp.isDone)
            {
                float progress = loadOp.progress * progressModifier;

                if (progress != previousProgress)
                {
                    onLoadProgressChanged.Invoke(progress);
                }

                previousProgress = progress;
                yield return(null);
            }

            Debug.Log("Loaded new scene: " + desired.SceneName);
        }
Exemple #2
0
        /// <summary>
        /// Retrieves the first scene that is considered a valid transition
        /// </summary>
        /// <param name="sceneManager">The collection of variables and scenes to be processed</param>
        /// <returns>The scene model of the first passing transition, or null if none pass</returns>
        public SceneModel GetFirstPassingTransition(SceneManagerController sceneManager)
        {
            foreach (SceneTransition transition in Transitions)
            {
                if (transition.IsMuted)
                {
                    continue;
                }

                SceneModel destScene = sceneManager.GetSceneById(transition.DestinationSceneId);

                if (destScene == null)
                {
                    Debug.LogError("Scene Index of " + transition.DestinationSceneId + " not found. Skipping transition");
                    continue;
                }

                if (!destScene.IsUnlocked)
                {
                    continue;
                }

                if (transition.CanTransition(sceneManager))
                {
                    return(destScene);
                }
            }

            return(null);
        }
        public void Evaluate()
        {
            ReevaluateLockStatuses();

            if (ActiveScene == null)
            {
                return;
            }

            SceneModel dest = ActiveScene.GetFirstPassingTransition(this);

            if (dest == null && ActiveScene.UseAnySceneTransitions)
            {
                dest = AnyScene.GetFirstPassingTransition(this);

                if (dest != null && dest.SceneId == ANY_SCENE_ID)
                {
                    dest = ActiveScene;
                }
            }
            if (dest != null)
            {
                TransitionToScene(dest);
            }
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="sceneManager">The controller that directed the transition</param>
 /// <param name="initialScene">The scene the transition was started on</param>
 /// <param name="destScene">The scene to be loaded</param>
 public SceneTransitionEventArgs(SceneManagerController sceneManager,
                                 SceneModel initialScene, SceneModel destScene)
 {
     SceneManager     = sceneManager;
     InitialScene     = initialScene;
     DestinationScene = destScene;
 }
        public int AddScene(string scenePath)
        {
            if (string.IsNullOrEmpty(scenePath))
            {
                throw new ArgumentException("Invalid scene path provided");
            }

            if (GetSceneByPath(scenePath) != null)
            {
                throw new ArgumentException("Scene with provided path already exists");
            }

            int sceneId = FindNextAvailableId();

            SceneModel newScene = new SceneModel(sceneId, scenePath);

            scenes.Add(newScene);

            if (entrySceneId < 0)
            {
                entrySceneId = newScene.SceneId;
            }

            return(newScene.SceneId);
        }
        public void TransitionToEntry()
        {
#if UNITY_EDITOR
            if (ignoreEntryCall)
            {
                return;
            }
#endif
            // If no entry scene has been set, just try to find the current scene
            if (EntryScene == null)
            {
                SceneModel model = scenes.Find((x) =>
                                               x.SceneAssetPath == SceneManager.GetActiveScene().path);

                ActiveScene = model;
                Evaluate();
                return;
            }

            // Check if the scene is already active
            if (SceneManagerExtensions.IsSceneLoadedByPath(EntryScene.SceneAssetPath))
            {
                ActiveScene = EntryScene;
                // it's already active, so do the starting evaluation
                Evaluate();
                return;
            }

            TransitionToScene(EntryScene);
        }
        protected override IEnumerator SwapScenes(SceneManagerController sceneManager,
                                                  SceneModel current, SceneModel desired)
        {
            yield return(new WaitForSeconds(WaitTime));

            yield return(base.SwapScenes(sceneManager, current, desired));
        }
        protected virtual IEnumerator SwapScenes(SceneManagerController sceneManager,
                                                 SceneModel current, SceneModel desired)
        {
            yield return(SceneManager.LoadSceneAsync(desired.SceneName, LoadSceneMode.Single));

            OnTransitionCompleted.SafeInvoke(
                new SceneTransitionEventArgs(sceneManager, current, desired));

            swapperCoroutine = null;
        }
        public void TransitionIfUnlocked(string sceneName)
        {
            SceneModel scene = GetSceneByName(sceneName);

            if (scene == null)
            {
                Debug.LogError("Failed to find scene " + sceneName + ", cannot transition");
                return;
            }

            TransitionIfUnlocked(scene);
        }
        private void OnEnable()
        {
            activeScene = null;

            if (sceneVariables == null)
            {
                sceneVariables = CreateInstance <SceneVariableContainer>();
            }

            sceneVariables.OnVariableChanged -= SceneVariables_OnVariableChanged;
            sceneVariables.OnVariableChanged += SceneVariables_OnVariableChanged;

            sceneVariables.ResetVariables();
        }
        public override bool Transition(SceneManagerController sceneManager,
                                        SceneModel current, SceneModel desired)
        {
            if (swapperCoroutine != null)
            {
                return(false);
            }

            OnTransitionStarted.SafeInvoke(new SceneTransitionEventArgs(sceneManager, current, desired));

            swapperCoroutine = StartCoroutine(SwapScenes(sceneManager, current, desired));

            return(true);
        }
        public void TransitionToScene(SceneModel desiredScene)
        {
            if (Transitioner == null)
            {
                Debug.LogError("Cannot transition scenes, transitioner is null");
                return;
            }

            if (desiredScene == null)
            {
                Debug.LogError("Target scene is null, cannot switch");
                return;
            }

            DoTransition(desiredScene);
        }
        protected virtual IEnumerator SwapScenes(SceneManagerController sceneManager,
                                                 SceneModel current, SceneModel desired)
        {
            float progressModifier = current == null ? 1f : 0.5f;

            yield return(LoadAdditiveSceneAsync(desired, progressModifier));

            if (current != null)
            {
                yield return(UnloadSceneAsync(current, 2f));
            }

            OnTransitionCompleted.SafeInvoke(
                new SceneTransitionEventArgs(sceneManager, current, desired));

            swapperCoroutine = null;
        }
        public SceneModel GetPreviousScene(SceneModel startScene, bool wrapAround = false, bool canReturnSelf = false)
        {
            int modelIndex = scenes.FindIndex((x) => x == startScene);

            if (modelIndex < 0)
            {
                return(null);
            }

            do
            {
                modelIndex--;

                if (modelIndex < 0)
                {
                    if (wrapAround)
                    {
                        modelIndex = scenes.Count - 1;
                    }
                    else
                    {
                        return(null);
                    }
                }

                SceneModel current = scenes[modelIndex];

                if (current.IncludeInIteration)
                {
                    return(current);
                }

                if (current == startScene)
                {
                    if (canReturnSelf)
                    {
                        return(startScene);
                    }
                    else
                    {
                        return(null);
                    }
                }
            } while (true);
        }
        private void Transitioner_OnSceneChanged(SceneTransitionEventArgs e)
        {
            if (e.DestinationScene == null)
            {
                Debug.LogError("Transitioned-to scene not provided, cannot set Active Scene");
                return;
            }

            SceneModel existingScene = GetSceneById(e.DestinationScene.SceneId);

            if (existingScene == null)
            {
                Debug.LogError("Transitioned-to scene not a part of the scene manager, cannot set Active Scene");
                return;
            }

            ActiveScene = existingScene;
            Evaluate();
        }
        public void TransitionIfUnlocked(SceneModel desiredScene)
        {
            if (Transitioner == null)
            {
                Debug.LogError("Cannot transition scenes, transitioner is null");
                return;
            }

            if (desiredScene == null)
            {
                Debug.LogError("Target scene is null, cannot switch");
                return;
            }

            if (desiredScene.IsUnlocked)
            {
                Transitioner.Transition(this, activeScene, desiredScene);
            }
        }
Exemple #17
0
        protected IEnumerator UnloadSceneAsync(SceneModel target, float progressModifier = 1f)
        {
            AsyncOperation unloadOp = SceneManager.UnloadSceneAsync(target.SceneName);

            float previousProgress = 0f;

            while (!unloadOp.isDone)
            {
                float progress = unloadOp.progress * progressModifier;

                if (progress != previousProgress)
                {
                    onLoadProgressChanged.Invoke(progress);
                }

                previousProgress = progress;
                yield return(null);
            }

            Debug.Log("Unloaded old scene: " + target.SceneName);
        }
        public SceneNode AddSceneNode(int sceneId, Vector2 position)
        {
            SceneModel sceneInfo = null;

            if (sceneId == SceneManagementUtility.SceneManagerController.ANY_SCENE_ID)
            {
                sceneInfo = SceneManager.AnyScene;
            }
            else
            {
                sceneInfo = SceneManager.GetSceneById(sceneId);
            }

            if (sceneInfo == null)
            {
                throw new ArgumentException("Failed to find scene under id: " + sceneId);
            }

            SceneNode node = new SceneNode(position, sceneInfo);

            sceneNodes.Add(node);
            return(node);
        }
        /// <summary>
        /// Determines if the transition to the destination scene is valid
        /// </summary>
        /// <param name="manager">The manager containing all of the variables to be evaluated</param>
        /// <returns>True if a transition can be performed, false if not
        /// Also returns false if DestinationScene is null</returns>
        public bool CanTransition(SceneManagerController manager)
        {
            SceneModel destinationScene = manager.GetSceneById(destinationSceneId);

            if (destinationScene == null)
            {
                return(false);
            }

            bool evaluation = true;

            foreach (SceneCondition condition in Conditions)
            {
                evaluation = condition.IsMet(manager);

                if (!evaluation)
                {
                    break;
                }
            }

            return(evaluation);
        }
Exemple #20
0
 public abstract bool Transition(SceneManagerController sceneManager,
                                 SceneModel current, SceneModel desired);
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="position">Position of the node</param>
 /// <param name="model">The scene model information to be stored with this node</param>
 public SceneNode(Vector2 position, SceneModel model)
     : base(position, model)
 {
     SceneId = model.SceneId;
 }
Exemple #22
0
 public SceneTransition CreateNewTransition(SceneModel destination)
 {
     return(CreateNewTransition(destination.SceneId));
 }
 private void DoTransition(SceneModel desiredScene)
 {
     Transitioner.Transition(this, ActiveScene, desiredScene);
 }
Exemple #24
0
 /// <summary>
 /// Find the transition model that represents a connection to the provided scene
 /// </summary>
 /// <param name="destScene">The scene model representing the desired destination scene</param>
 /// <returns>The first matching transition, or null if none found</returns>
 public SceneTransition GetFirstTransitionTo(SceneModel destScene)
 {
     return(GetFirstTransitionTo(destScene.SceneId));
 }