Created
November 13, 2024 16:28
-
-
Save vbayeva/aad42664d8d94e240b3cc89e6806fecf to your computer and use it in GitHub Desktop.
Revisions
-
vbayeva created this gist
Nov 13, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ public class ExitTrigger : MonoBehaviour { [SerializeField] private string levelName; private void OnTriggerEnter(Collider other) { if (other.tag == "Player") { GameEvents.InvokeSceneChange(levelName); } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ public class GameController : MonoBehaviour { [SerializeField] private SceneController sceneController; private void Awake() { GameEvents.OnSceneChanged += LoadScene; } private void LoadScene(string levelName) { Debug.Log(levelName); sceneController.LoadScene(levelName); // make music change // stop player's movement } private void Start() { sceneController.LoadScene("Level_1"); } [ProPlayButton] private void LoadScene() { sceneController.LoadScene("Level_1"); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ public static class GameEvents { public static event Action<string> OnSceneChanged; public static void InvokeSceneChange(string sceneName) { OnSceneChanged?.Invoke(sceneName); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ public class SceneController : MonoBehaviour { [Header("References")] [SerializeField] private GameObject loadingCanvas; [SerializeField] private TextMeshProUGUI tipText; [SerializeField] private Slider progressBar; [Header("Time")] [SerializeField] private float minimumLoadingTime = 5.0f; [Header("Tips")] [SerializeField] private List<string> tips; private string _previousLevel = ""; private void Start() { loadingCanvas.SetActive(false); } public void LoadScene(string levelName) { StartCoroutine(LoadSceneAsync(levelName)); } private IEnumerator LoadSceneAsync(string levelName) { Scene scene = SceneManager.GetSceneByName(levelName); if (scene.IsValid() && scene.isLoaded) { yield break; } if (_previousLevel != "") { SceneManager.UnloadSceneAsync(_previousLevel); } _previousLevel = levelName; loadingCanvas.SetActive(true); tipText.text = tips[UnityEngine.Random.Range(0, tips.Count)]; AsyncOperation operation = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Additive); operation.allowSceneActivation = false; float startTime = Time.time; while (!operation.isDone) { var _loadingProgress = Mathf.Min((Time.time - startTime) / minimumLoadingTime, Mathf.Clamp01(operation.progress / 0.9f)); progressBar.value = _loadingProgress; if (Time.time - startTime >= minimumLoadingTime && operation.progress >= 0.9f) { operation.allowSceneActivation = true; } yield return null; } loadingCanvas.SetActive(false); } }