Unity 进度条

来源:互联网 发布:淘宝网首页布局介绍 编辑:程序博客网 时间:2024/05/01 18:31

Unity 进度条

协程异步加载场景

using UnityEngine;using System.Collections;using UnityEngine.SceneManagement;using System;using UnityEngine.UI;public class LoadScence : MonoBehaviour{    Slider slider;    Text text;    private void Awake()    {        slider = transform.Find("Slider").GetComponent<Slider>();        text= transform.Find("Text").GetComponent<Text>();    }    private void Start()    {        StartCoroutine(StartLoading());//开启协程    }    private IEnumerator StartLoading()//协程    {        int displayProgress = 0;        int toProgress = 0;        AsyncOperation scene = SceneManager.LoadSceneAsync(2);//异步加载场景        scene.allowSceneActivation = false;    //不允许场景在准备就绪后立即被激活        while (scene.progress < 0.9f)        {            toProgress = (int)scene.progress * 100;//progress 0-1            while (displayProgress < toProgress)            {                ++displayProgress;                slider.value = displayProgress *0.01f;                this.text.text = "Loading..." + displayProgress.ToString()+"%";                yield return new WaitForEndOfFrame();//等待直到所有的摄像机和GUI被渲染完成            }        }        toProgress = 100;        while (displayProgress <= toProgress)        {            yield return new WaitForEndOfFrame();//等待直到所有的摄像机和GUI被渲染完成            ++displayProgress;             slider.value = displayProgress*0.01f;            this.text.text = "Loading..." + displayProgress.ToString() + "%";        }        scene.allowSceneActivation = true;//允许场景在准备就绪后立即被激活    }}

这里写图片描述