Unity学习-------异步场景加载(AsyncOperation)

Posted HaryCoding

tags:

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

首先,学习异步场景加载之前,先了解什么是异步场景加载。

一、什么是异步场景加载

       在目前市面上基本所有的游戏都使用了这个方法,举个例子

      

     

       (绝地求生加载画面)

       

          (英雄联盟加载画面)

        从这两个例子不难看出异步加载无非就是让玩家等待游戏进度条加载。

二、异步场景加载的学习

        这边就简单介绍如何使用这个方法。

   1、首先准备两个场景,一个作为主界面,一个作为游戏跳转界面

        

   2、设计两个UI板块,一个作为交互界面,一个作为异步场景加载界面

 

          我这边简单操作一下

(按下按钮Loadpanel 界面隐藏,显示Background界面,当数值加载至100%,跳转到游戏场景)

      3、上脚本

          做完前面几步,写一个关于的AsyncOperation脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Begin : MonoBehaviour

    public GameObject Background;
    //异步场景加载界面
    public Text text;
    //获取加载界面的文本
    public Slider slider;
    //设置共有对象来改变slider的value值

    public void loadNext()
    
        StartCoroutine(loadlevel());
        //使用loadlevel方法
    

    IEnumerator loadlevel()
    //设置协程类型方法loadlevel
    
        Background.SetActive(true);

        AsyncOperation operation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
        //加载本场景数值 +1的场景SampleScene
        operation.allowSceneActivation = false;
        //先不加载下一场景
        while (!operation.isDone)
        
            slider.value = operation.progress;
            //operation.progress本质上就是数值
            text.text = operation.progress * 100 + "%";

            if (operation.progress >= 0.9f)
            //由于该方法本身的问题所以需要手动把数值改为100%
            
                slider.value = 1;
                text.text = "Press any key";
                if (Input.anyKeyDown)
                
                    operation.allowSceneActivation = true;
                    //按下任意按钮开始加载下一场景
                
            
            yield return null;
        
    

简单展示一下

 说明:这个游戏比较小,所以异步加载界面数值变化不明显。

 

 

 

 

 

 

 

 

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 }

 

以上是关于Unity学习-------异步场景加载(AsyncOperation)的主要内容,如果未能解决你的问题,请参考以下文章

Unity 异步加载场景

unity3d 异步加载场景代码

Unity异步加载关卡

Unity中场景异步加载

unity中 加载大场景怎么解决

Unity笔记使用协程(Coroutine)异步加载场景