A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
SceneControllerManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using UnityEngine;
4using UnityEngine.SceneManagement;
5using UnityEngine.UI;
6
10public class SceneControllerManager : SingletonMonobehaviour<SceneControllerManager>
11{
12
13 [SerializeField] private float m_FadeDuration = 1.0f;
14 [SerializeField] private CanvasGroup m_FaderCanvasGroup = null;
15 [SerializeField] private Image m_FaderImage = null;
16
17 private bool m_IsFading;
18
20
26 private IEnumerator Fade( float finalAlpha )
27 {
28 // Set the fading flag to true so the FadeAndSwitchScenes coroutine won't be called again.
29 m_IsFading = true;
30
31 // Make sure the CanvasGroup blocks raycast into the scene so no more input can be accepted.
32 m_FaderCanvasGroup.blocksRaycasts = true;
33
34 // Calculate how fast CanvasGroup should fade based on it's current alpha, it's final alpha and how long it has to change between two.
35 float fadeSpeed = Mathf.Abs( m_FaderCanvasGroup.alpha - finalAlpha) / m_FadeDuration;
36
37 // While the CanvasGroup hasn't reached the final alpha yet...
38 while( !Mathf.Approximately( m_FaderCanvasGroup.alpha, finalAlpha) )
39 {
40 // ...Move the alpha towards it's target alpha.
41 m_FaderCanvasGroup.alpha = Mathf.MoveTowards( m_FaderCanvasGroup.alpha, finalAlpha, fadeSpeed * Time.deltaTime );
42
43 // Wait for a frame then continue.
44 yield return null;
45 }
46
47 // Set the flag to false since the fade has finished
48 m_IsFading = false;
49
50 // Stop the CanvasGroup from blocking raycasts so input is no longer ignored.
51 m_FaderCanvasGroup.blocksRaycasts = false;
52 }
53
60 private IEnumerator FadeAndSwitchScenes( string sceneName, Vector3 spawnPosition )
61 {
62 // Call before scene unload fade out event.
64
65 // Start fading to black and wait for it to finish before continuing.
66 yield return StartCoroutine( Fade( 1.0f ) );
67
68 // Store Scene Data
69 SaveLoadManager.Instance.StoreCurrentSceneData();
70
71 // Set player position
72 Player.Instance.gameObject.transform.position = spawnPosition;
73
74 // Call before scene unload event
76
77 // Unload the current active scene.
78 yield return SceneManager.UnloadSceneAsync( SceneManager.GetActiveScene().buildIndex );
79
80 // Start loading the given scene and wait for it to finish.
81 yield return StartCoroutine( LoadSceneAndSetActive( sceneName ) );
82
83 // Call after scene load event.
85
86 // Restore new scene data
87 SaveLoadManager.Instance.RestoreCurrentSceneData();
88
89 // Start fading back in and wait for it to finish before exiting the function.
90 yield return StartCoroutine( Fade(0.0f) );
91
92 // Call after scene load fade in event.
94 }
95
102 private IEnumerator LoadSceneAndSetActive( string sceneName )
103 {
104 // Allow the given scene to load over several frames and add it to the already loaded scenes ( just the Persistent scene at this point).
105 yield return SceneManager.LoadSceneAsync( sceneName, LoadSceneMode.Additive );
106
107 // Find the scene that was most recently loaded (the one at the last index of the loaded scenes).
108 Scene newlyLoadedScene = SceneManager.GetSceneAt( SceneManager.sceneCount - 1 );
109
110 // Set the newly loaded scene as the active scene (this marks it as the one to be unloaded next).
111 SceneManager.SetActiveScene( newlyLoadedScene );
112 }
113
118 private IEnumerator Start()
119 {
120 // Set the initial alpha to start off with a black screen.
121 m_FaderImage.color = new Color( 0f, 0f, 0f, 1f );
122 m_FaderCanvasGroup.alpha = 1f;
123
124 // Star the first scene loading and wait for it to finish.
125 yield return StartCoroutine( LoadSceneAndSetActive( m_StartingSceneName.ToString() ) );
126
127 // If this event has any subscribers, call it.
129
130 // Restore new scene data
131 SaveLoadManager.Instance.RestoreCurrentSceneData();
132
133 // Once the scene is finished loading, start fading in.
134 StartCoroutine( Fade( 0f ) );
135
136 }
137
144 public void FadeAndLoadScene( string sceneName, Vector3 spawnPosition )
145 {
146 // If a fade isn't happening then start fading and switching scenes.
147 if( !m_IsFading )
148 {
149 StartCoroutine( FadeAndSwitchScenes( sceneName, spawnPosition ) );
150 }
151 }
152}
SceneName
Describes different scenes.
Definition: Enums.cs:118
Raise an handle event when a movement is triggered.
Definition: EventHandler.cs:15
static void CallAfterSceneLoadEvent()
Call after scene load event
static void CallBeforeSceneUnloadEvent()
Call before scene unload event
static void CallBeforeSceneUnloadFadeOutEvent()
Call before scene unload, fade out event
static void CallAfterSceneLoadFadeInEvent()
Call after scene load fade in event
Manage saving and loading scenes. This class is a singleton instance.
Manage the transition between scenes
void FadeAndLoadScene(string sceneName, Vector3 spawnPosition)
This is the main externam point of contact and influence from the rest of the project....
IEnumerator Fade(float finalAlpha)
Handle the fade in blocking and unblocking inputs.
IEnumerator Start()
Load the starting scene
IEnumerator LoadSceneAndSetActive(string sceneName)
Load the scene and the activate it. Basically, we are additing our new scene to the Persistent scene.
IEnumerator FadeAndSwitchScenes(string sceneName, Vector3 spawnPosition)
Handle fading, switching and unloading scenes.
Abstract class that inherited by all the game managers. All the game managers use singleton since gam...