当前位置:   article > 正文

Unity 3D开发--SceneManager场景管理(异步使用同一个过渡场景)_unity scenemanager

unity scenemanager

U3D开发过程中经常使用到多场景的切换,有同步SceneManager.LoadScene()和异步SceneManager.LoadSceneAsync()两种方法,同步的话一般就会卡住界面直到加载完成,使用异步的话一般都做一个加载的进度条,每次切换的时候都需要一个加载动画,所以需要建一个专门的过渡加载场景来进行统一加载,也可以避免场景直接切换出现的黑屏。

一、建立一个单例进行切换,在项目代码的任何位置都可以调用

  1. public class LoadSceneManager
  2. {
  3. private static LoadSceneManager instance;
  4. public static LoadSceneManager Instance
  5. {
  6. get {
  7. if (instance == null)
  8. {
  9. instance = new LoadSceneManager();
  10. }
  11. return instance;
  12. }
  13. }
  14. public string nextSceneName;
  15. //异步加载
  16. public void LoadSceneAsync(string sceneName)
  17. {
  18. Debug.Log("LoadSceneAsync:" + sceneName);
  19. nextSceneName = sceneName;
  20. SceneManager.LoadScene("LoadingScene");
  21. }
  22. public void LoadScene(string sceneName)
  23. {
  24. Debug.Log("LoadScene:" + sceneName);
  25. SceneManager.LoadScene(sceneName);
  26. }
  27. }

使用的时候直接使用LoadSceneManager.Instance.LoadSceneAsync("SecondScene");

二、异步加载的过程

要做个进度条然后进行管理,代码很简单就不说了

  1. public class LoadingController : MonoBehaviour
  2. {
  3. public Slider loadingSlider;
  4. public TMP_Text loadingText;
  5. private AsyncOperation asyncOperation;
  6. private float operationProgress;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. loadingSlider.value = 0.0f;
  11. if (SceneManager.GetActiveScene().name == "LoadingScene")
  12. {
  13. StartCoroutine("LoadingScene");
  14. }
  15. }
  16. IEnumerator LoadingScene()
  17. {
  18. asyncOperation = SceneManager.LoadSceneAsync(LoadSceneManager.Instance.nextSceneName);
  19. asyncOperation.allowSceneActivation = false;
  20. yield return asyncOperation;
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. operationProgress = asyncOperation.progress;
  26. //最大值只到0.9,后面有进行插值运算更新
  27. if (Mathf.Approximately(operationProgress, 0.9f))
  28. {
  29. operationProgress = 1;
  30. }
  31. if (Mathf.Approximately(operationProgress, 0.6f))
  32. {
  33. System.Threading.Thread.Sleep(5000);
  34. }
  35. UpdateLoadingUI(operationProgress);
  36. }
  37. private void UpdateLoadingUI(float value)
  38. {
  39. if (operationProgress != loadingSlider.value)
  40. {
  41. loadingSlider.value = Mathf.Lerp(loadingSlider.value, operationProgress, Time.deltaTime * 2);
  42. if (Mathf.Approximately(operationProgress, operationProgress))
  43. {
  44. loadingSlider.value = operationProgress;
  45. }
  46. }
  47. Debug.Log("Progress:" + loadingSlider.value);
  48. if (loadingText != null)
  49. {
  50. loadingText.text = Mathf.Round(loadingSlider.value * 100) + "%";
  51. }
  52. //自动换场景
  53. if (Mathf.Approximately(loadingSlider.value, 1.0f))
  54. {
  55. asyncOperation.allowSceneActivation = true;
  56. }
  57. }
  58. }

三、demo链接

https://download.csdn.net/download/emailforwei/87329335


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/99786
推荐阅读
相关标签
  

闽ICP备14008679号