using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.Events; using UnityEngine.SceneManagement; using UnityEngine.UI; public class SceneLoader : MonoBehaviour { public static SceneLoader Instance; public List ztPositions; public List ztRotations; /// /// 当前展厅索引 /// public static int currentIndex = -1; public Slider slider; // 滑动条 int currentProgress; // 当前进度 int targetProgress; // 目标进度 public bool isAllLoaded; void Awake() { Instance = this; } private void Start() { currentProgress = 0; targetProgress = 0; GameObject obj = GameObject.Find("BtnExLoader" + currentIndex); if (obj != null) { obj.GetComponent().color = Color.cyan; } } AsyncOperation asyncOperation; /// /// 异步加载场景 /// public IEnumerator LoadScene(int index) { if (index != currentIndex) { if (isAllLoaded) { SetCameraPosition(index); AudioManager.Instance.PlayBg(index); } else { slider.gameObject.SetActive(true); asyncOperation = SceneManager.LoadSceneAsync("ZT" + index); // 不允许场景立即激活 // 异步进度在 allowSceneActivation= false时,会卡在0.89999的一个值 asyncOperation.allowSceneActivation = false; // 当异步加载小于0.9f的时候 while (asyncOperation.progress < 0.9f) { targetProgress = (int)(asyncOperation.progress * 100); yield return LoadProgress(); } // 循环后,当前进度已经为90了,所以需要设置目标进度到100;继续循环 targetProgress = 100; yield return LoadProgress(); currentIndex = index; asyncOperation.allowSceneActivation = true; asyncOperation.completed += AsyncOperation_completed; slider.gameObject.SetActive(false); currentProgress = 0; targetProgress = 0; } } } private void AsyncOperation_completed(AsyncOperation obj) { AudioManager.Instance.PlayBg(currentIndex); } public void SetCameraPosition(int index) { NavMeshAgent agent = FindObjectOfType(); if (agent != null) agent.enabled = false; if (ztPositions.Count > index) { CommonData.MainCamera.transform.localPosition = ztPositions[index]; } if (ztRotations.Count > index) { CommonData.MainCamera.transform.localRotation = Quaternion.Euler(ztRotations[index]); } if (agent != null) agent.enabled = true; } ShowPoint currentSPInfo; public void SetCameraPosition(ShowPoint info) { //if (currentIndex != info.ZT_Index) //{ // currentIndex = info.ZT_Index; // AudioManager.Instance.PlayBg(currentIndex); //} currentSPInfo = info; LoadSceneData(info.ZT_Index, SetPostionOnLoaded ); } void SetPostionOnLoaded() { SetCameraPosition(currentSPInfo.Position, currentSPInfo.Rotation); } public void SetCameraPosition(Vector3 pos, Vector3 rot) { NavMeshAgent agent = FindObjectOfType(); if (agent != null) agent.enabled = false; CommonData.MainCamera.transform.localPosition = pos; CommonData.MainCamera.transform.localRotation = Quaternion.Euler(rot); if (agent != null) agent.enabled = true; } /// /// 由于需要两次调用,在这里进行简单封装 /// /// 等一帧 private IEnumerator LoadProgress() { yield return new WaitForEndOfFrame(); // 当前进度 < 目标进度时 while (currentProgress < targetProgress) { ++currentProgress; slider.value = (float)currentProgress / 100; yield return new WaitForEndOfFrame(); } } public void LoadSceneData(int newIndex, UnityAction onDataLoaded) { if (newIndex != SceneLoader.currentIndex) { SceneLoader.currentIndex = newIndex; LoadData(newIndex, onDataLoaded); AudioManager.Instance.PlayBg(newIndex); } else { if (onDataLoaded != null) { onDataLoaded(); } } } void LoadData(int index, UnityAction onDataLoaded) { StartCoroutine(DataLoader.LoadDataDictionary(string.Format("SpotsZT{0}.json", index), onDataLoaded)); StartCoroutine(DataLoader.LoadDataList(string.Format("Data_ZT{0}.json", index))); } }