码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-18 23:07:03      阅读:746      评论:0      收藏:0      [点我收藏+]

标签:target   sys   对象   mono   进度条   targe   div   art   ati   

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;
        }
    }

}

 

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

标签:target   sys   对象   mono   进度条   targe   div   art   ati   

原文地址:http://www.cnblogs.com/guxin/p/unity-use-coroutine-asynchronously-load-scene.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!