zoukankan      html  css  js  c++  java
  • 【Unity笔记】使用协程(Coroutine)异步加载场景

    using UnityEngine;
    using System.Collections;
    using UnityEngine.SceneManagement;
    using System;
    
    public class LoadingPage : MonoBehaviour {
    
        public UISlider progressBar;
    
        // 目标进度
        float target = 0;
        // 读取场景的进度,取值范围0~1
        float progress = 0;
        // 异步对象
        AsyncOperation op = null;
    
        void Start () {
            Debug.Log("开始LoadScene");
    
            op = SceneManager.LoadSceneAsync("GamePlayScene");
            op.allowSceneActivation = false;
            progressBar.value = 0;
    
            // 开启协程,开始调用加载方法
            StartCoroutine(processLoading());
        }
    
        float dtimer = 0;
        void Update()
        {
            progressBar.value = Mathf.Lerp(progressBar.value, target, dtimer * 0.02f);
            dtimer += Time.deltaTime;
            if (progressBar.value > 0.99f)
            {
                progressBar.value = 1;
                op.allowSceneActivation = true;
            }
        }
    
        // 加载进度
        IEnumerator processLoading()
        {
            while (true)
            {
                target = op.progress; // 进度条取值范围0~1
                if (target >= 0.9f)
                {
                    target = 1;
                    yield break;
                }
                yield return 0;
            }
        }
    
    }
  • 相关阅读:
    storm中的Scheduler
    开启flume的远程调试功能
    修改flume源码,使其HTTPSource具备访问路径功能
    非功能测试——效率测试
    python100例
    awk命令
    shell正则表达式
    python的垃圾回收机制
    冯-诺伊曼体系结构
    jmeter读取文件内容做变量
  • 原文地址:https://www.cnblogs.com/guxin/p/unity-use-coroutine-asynchronously-load-scene.html
Copyright © 2011-2022 走看看