ITeye博客

Unity3D异步加载游戏场景

wgcode · 2015-05-14 09:47 · Unity · 移动开发 · Markdown

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Test : MonoBehaviour
  4. {
  5. public UISlider slider;
  6. AsyncOperation async;
  7. bool flag = false;
  8. // Use this for initialization
  9. void Start()
  10. {
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. if (flag)
  16. {
  17. slider.value = async.progress;
  18. }
  19. }
  20. void OnClick()
  21. {
  22. StartCoroutine(LoadScene());
  23. }
  24. IEnumerator LoadScene()
  25. {
  26. async = Application.LoadLevelAsync("Scene");
  27. yield return async;
  28. flag = true;
  29. }
  30. }

 

加载场景但不激活

 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Test : MonoBehaviour
  4. {
  5. public UISlider progressSlider;
  6. bool flag = false;
  7. AsyncOperation async;
  8. // Use this for initialization
  9. void Start()
  10. {
  11. StartCoroutine(LoadScene());
  12. }
  13. // Update is called once per frame
  14. void Update()
  15. {
  16. if (flag && progressSlider.value!=1)
  17. {
  18. progressSlider.value = async.progress;
  19. }
  20. }
  21. void OnClick()
  22. {
  23. async.allowSceneActivation = true;
  24. }
  25. IEnumerator LoadScene()
  26. {
  27. async = Application.LoadLevelAsync("Scene");
  28. async.allowSceneActivation = false;
  29. flag = true;
  30. yield return async;
  31. }
  32. }

此处有个bug,或许是我做的不对,async.progress最大只能到9。

 

http://forum.unity3d.com/threads/using-allowsceneactivation.166106/

导出webplayer版发现进入第一个场景后,第二个场景没有预加载,原因是勾选了Streamed。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。