123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using System;
- public class GameManager : MonoBehaviour {
- public static GameManager instance;
- [SerializeField]
- private Fade fade;
- [SerializeField]
- private Text loadText;
- [SerializeField]
- private Image loadBar;
- [SerializeField]
- private String firstSceneName;
- [SerializeField]
- private Camera _loadingCamera;
-
- private bool isLodaingScene;
- private void Awake()
- {
- if (instance) { Destroy(this.gameObject); }
- else
- {
- instance = this;
- }
- }
-
- private void Start()
- {
- StartCoroutine(ChangeScene(firstSceneName));
- }
-
-
-
-
- public void SwitchScene(string sceneName)
- {
- StartCoroutine(ChangeScene(sceneName));
- }
-
-
-
-
-
- private IEnumerator ChangeScene(string sceneName)
- {
- isLodaingScene = true;
-
- if (SceneManager.sceneCount >= 2)
- {
- fade.gameObject.SetActive(true);
- yield return fade.FadeOut();
- }
-
- while (SceneManager.sceneCount >= 2)
- {
- yield return SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
- }
-
- yield return LoadSceneAsync(sceneName);
- isLodaingScene = false;
-
- yield return fade.FadeIn();
- fade.gameObject.SetActive(false);
- }
-
-
-
-
-
- private IEnumerator LoadSceneAsync(string sceneName)
- {
- int displayProgress = 0;
- int toProgress = 0;
- setLoadingEffct(true);
- AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
- asyncOperation.allowSceneActivation = false;
-
- while (asyncOperation.progress < 0.9f)
- {
- toProgress = (int)asyncOperation.progress * 100;
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetLoadingPercentage(displayProgress);
- yield return new WaitForEndOfFrame();
- }
- }
- toProgress = 100;
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetLoadingPercentage(displayProgress);
- yield return new WaitForEndOfFrame();
- }
-
- asyncOperation.allowSceneActivation = true;
- yield return new WaitForSeconds(1f);
- yield return asyncOperation.isDone;
-
- SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
- setLoadingEffct(false);
- }
-
-
-
-
- void SetLoadingPercentage(int progress)
- {
- loadText.text = progress + "%";
- loadBar.fillAmount = progress / 100f;
- }
-
-
-
-
- void setLoadingEffct(bool flag)
- {
- _loadingCamera.enabled = flag;
- _loadingCamera.GetComponent<AudioListener>().enabled = flag;
- loadText.enabled = flag;
- loadBar.enabled = flag;
- }
- }
|