不填加新场景直接在本场景实现异步加载
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class LoadScene : MonoSingleton<LoadScene> { public Button SystemTeachingModeButton;// 系统教学按钮 public Button TeachingModeButton;// 教学模式按钮 public Button PracticeModeButton;// 练习模式按钮 public Button AssessmentModeButton;// 考核模式按钮 public Image progressImage; private float loadingSpeed = 1; void Update() { if (asyncOperation != null) { targetValue = asyncOperation.progress; if (asyncOperation.progress >= 0.9f) targetValue = 1.0f; if (targetValue != progressImage.fillAmount) { progressImage.fillAmount = Mathf.Lerp(progressImage.fillAmount, targetValue, Time.deltaTime * loadingSpeed); if (Mathf.Abs(progressImage.fillAmount - targetValue) < 0.01f) progressImage.fillAmount = targetValue; } progressText.text = ((int)(progressImage.fillAmount * 100)).ToString() + "%"; if ((int)(progressImage.fillAmount * 100) == 100) asyncOperation.allowSceneActivation = true; } } IEnumerator LoadSceneManager(string sceneName) { if (isLoading) yield break; asyncOperation = SceneManager.LoadSceneAsync(sceneName); asyncOperation.allowSceneActivation = false; isLoading = true; GlobalConfig.ResetAll.Reset(); yield return asyncOperation; } public bool isLoadingSelect; IEnumerator LoadSelectSceneManager() { if (isLoadingSelect) yield break; float i = 0; AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(TypeOfEnums.ScenesName.SelectScene.ToString()); asyncOperation.allowSceneActivation = false; while (i < 100) { i++; yield return new WaitForEndOfFrame(); } yield return new WaitForSeconds(0.5f); isLoadingSelect = false; asyncOperation.allowSceneActivation = true; } } public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour { private static T instance; public static T Instance { get { if (instance == null) { GameObject obj = new GameObject(typeof(T).Name); instance = obj.AddComponent<T>(); } return instance; } } protected virtual void Awake() { instance = this as T; } }