-
-
Save disini/17c6a8bdc8b16d0e4307c4b1cfc7af01 to your computer and use it in GitHub Desktop.
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 characters
| 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 characters
| 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 characters
| 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 characters
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment