123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using System;
- [Serializable]
- public class SceneBGM
- {
- public string Name;
- public AudioClip clip;
- }
- public class SceneController : MonoBehaviour
- {
- private static SceneController instance;
- public static SceneController Instacne
- {
- get
- {
- if(!instance)
- {
- instance = FindObjectOfType<SceneController>();
- }
- if(!instance)
- {
- Debug.Log("场景控制物体不存在!");
- }
- return instance;
- }
- }
- public bool IsLoadingScene
- {
- get
- {
- return isLodaingScene;
- }
- }
- [SerializeField]
- private Fade fade;
- [SerializeField]
- private Text loadText;
- [SerializeField]
- private String firstSceneName;
- [SerializeField]
- private GameObject loadAnim;
- [SerializeField]
- private Text loadUI;
- [SerializeField,Header("背景音乐")]
- List<SceneBGM> BGMS;
-
- AudioSource BGMaudio;
- private bool isLodaingScene;
-
- private void Awake()
- {
- BGMaudio = GetComponent<AudioSource>();
- }
- private void Start()
- {
- StartCoroutine(ChangeScene(firstSceneName));
- loadText.text = "Please Wait";
- }
-
-
-
-
-
- public void SwitchScene(string sceneName)
- {
- StartCoroutine(ChangeScene(sceneName));
- }
-
-
-
-
-
- private IEnumerator ChangeScene(string sceneName)
- {
- isLodaingScene = true;
-
- if (SceneManager.sceneCount >= 2)
- {
- yield return fade.FadeOut();
- }
-
- while(SceneManager.sceneCount >= 2)
- {
- yield return SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
- }
-
- yield return LoadSceneAsync(sceneName);
-
- BGMChange(sceneName);
- isLodaingScene = false;
-
- yield return fade.FadeIn();
- }
-
-
-
-
-
-
- private IEnumerator LoadSceneAsync(string sceneName)
- {
- int currProgress = 0;
- int showProgress = 0;
- loadText.enabled = true;
- loadAnim.SetActive(true);
- loadUI.enabled = true;
- AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
- async.allowSceneActivation = false;
-
- while (async.progress<0.9f)
- {
- currProgress = (int)(async.progress * 100);
- while (showProgress < currProgress)
- {
- showProgress++;
- setProgressValue(showProgress);
- yield return new WaitForEndOfFrame();
- }
-
-
-
- yield return null;
- }
- currProgress = 100;
- while (showProgress < currProgress)
- {
- showProgress++;
- setProgressValue(showProgress);
- yield return new WaitForEndOfFrame();
- }
- async.allowSceneActivation = true;
- yield return async.isDone;
- SceneManager.SetActiveScene(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
- loadAnim.SetActive(false);
- loadText.enabled = false;
- loadUI.enabled = false;
- }
- void setProgressValue(int value)
- {
- loadUI.text = value + "%";
- }
-
-
-
-
- void BGMChange(string name)
- {
- for (int i = 0; i < BGMS.Count; i++)
- {
- if (BGMS[i].Name == name)
- {
- BGMaudio.clip = BGMS[i].clip;
- BGMaudio.Play();
- BGMaudio.loop = true;
- }
- }
-
- }
-
-
-
-
- public void BGMvolume(float volume)
- {
- BGMaudio.volume = volume;
- }
- }
|