---
title: "Unity3D异步加载游戏场景"
author: "wgcode"
date: "2015-05-14T09:47:21Z"
tags: []
categories: ["移动开发"]
slug: "unity3d"
draft: false
lastmod: "2015-05-13T14:41:21Z"
id: 2210793
url: "https://tts.iteye.com/blog/2210793"
---

**\[csharp]** [view plain](http://blog.csdn.net/awnuxcvbn/article/details/38543855 "view plain")[copy](http://blog.csdn.net/awnuxcvbn/article/details/38543855 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/449832 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/449832/fork "派生到我的代码片")

 

01. using UnityEngine;
02. using System.Collections;
03. public class Test : MonoBehaviour
04. {
05. public UISlider slider;
06. AsyncOperation async;
07. bool flag = false;
08. // Use this for initialization
09. 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. }

![](http://img.blog.csdn.net/20140813221336746?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvQVdOVVhDVkJO%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

![](http://img.blog.csdn.net/20140813221529721?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvQVdOVVhDVkJO%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

 

加载场景但不激活

 

**\[csharp]** [view plain](http://blog.csdn.net/awnuxcvbn/article/details/38543855 "view plain")[copy](http://blog.csdn.net/awnuxcvbn/article/details/38543855 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/449832 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/449832/fork "派生到我的代码片")

 

01. using UnityEngine;
02. using System.Collections;
03. public class Test : MonoBehaviour
04. {
05. public UISlider progressSlider;
06. bool flag = false;
07. AsyncOperation async;
08. // Use this for initialization
09. 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/](http://forum.unity3d.com/threads/using-allowsceneactivation.166106/)

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