unity3d 异步加载场景代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity3d 异步加载场景代码相关的知识,希望对你有一定的参考价值。

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 加载场景
 6 /// </summary>
 7 public class LoadSence : MonoBehaviour {
 8 
 9 
10     private IEnumerator StartLoading(string SenceName)
11     {
12         int displayProgress = 0;
13         int toProgress = 0;
14         AsyncOperation op = Application.LoadLevelAsync(SenceName);
15         op.allowSceneActivation = false;
16         while (op.progress < 0.2f)
17         {
18             toProgress = (int)op.progress * 100;
19             while (displayProgress < toProgress)
20             {
21                 ++displayProgress;
22                 SetLoadingPercentage(displayProgress);
23                 yield return new WaitForEndOfFrame();
24             }
25         }
26 
27         toProgress = 100;
28         while (displayProgress < toProgress)
29         {
30             ++displayProgress;
31             SetLoadingPercentage(displayProgress);
32             yield return new WaitForEndOfFrame();
33         }
34         yield return new WaitForSeconds(1.0f);
35         op.allowSceneActivation = true;
36     }
37 
38 
39 
40     // Use this for initialization
41     void Start () {
42 
43         Time.timeScale = 1.0f;
44 
45         switch (GameManager.CurrentSenceIndex)
46         {
47     
48             case 1:
49         
50                 GameManager.m_NextSence = "linjiankongdi";
51 
52                 break;
53             case 2:
54                 GameManager.m_NextSence =  "linjiankongdi2";    
55     
56                 break;
57             case 3:
58 
59                 GameManager.m_NextSence =  "huangyejiaotang";        
60 
61                 break;
62             case 4:
63 
64                 GameManager.m_NextSence =  "huangyejiaotang2";    
65 
66                 break;
67 
68             default:
69                 break;
70         }
71 
72         StartCoroutine(StartLoading(GameManager.m_NextSence));
73         //StartCoroutine(StartLoading("SenceOne"));
74 
75     }
76 
77     public UISlider m_UISlider;
78 
79     void SetLoadingPercentage(int progress)
80     {
81         m_UISlider.value = (float)progress / 100.0f;
82     }
83 
84 
85 
86 
87     // Update is called once per frame
88     void Update () {
89     
90     }
91 }

 

以上是关于unity3d 异步加载场景代码的主要内容,如果未能解决你的问题,请参考以下文章

unity3d 怎样优化大场景模型

Unity3d C# 实现AA包(Addressables)资源热更新的多个包异步加载并显示加载实时进度功能(含源码)

如何在Unity3D使用 WWW 加载场景并显示进度条

Unity3d中制作Loading场景进度条所遇到的问题 LoadLevelAsync,AsyncOperation

在unity3d里怎么做进度条

如何在Unity3D使用 WWW 加载场景并显示进度条