【Unity】场景跳转,场景异步加载

            this.GetComponent

将此代码挂在子物体含进度条的物体上

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

/*
SmallGame
时间:2017.8.12
作者:烧仙草奶茶
*/
/// 
/// 加载场景 —— 脚本挂载前场景
/// 
public class LoadScene : MonoBehaviour
{
    private Slider slider;          //滑动条
    int currentProgress; //当前进度
    int targetProgress;  //目标进度
    public int number=1;

    private void Start()
    {
        currentProgress = 0;
        targetProgress = 0;
        slider = GameObject.Find("Slider").GetComponent();
        StartCoroutine(LoadingScene()); //开启协成
    }


    /// 
    /// 异步加载场景
    /// 
    /// 协成
    private IEnumerator LoadingScene()
    {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(number); //异步加载1号场景
        asyncOperation.allowSceneActivation = false;                          //不允许场景立即激活//异步进度在 allowSceneActivation= false时,会卡在0.89999的一个值,这里乘以100转整形
        while (asyncOperation.progress < 0.9f)                                //当异步加载小于0.9f的时候
        {
            targetProgress = (int)(asyncOperation.progress * 100); //异步进度在 allowSceneActivation= false时,会卡在0.89999的一个值,这里乘以100转整形
            yield return LoadProgress();
        }
        targetProgress = 100; //循环后,当前进度已经为90了,所以需要设置目标进度到100;继续循环
        yield return LoadProgress();
        asyncOperation.allowSceneActivation = true; //加载完毕,这里激活场景 —— 跳转场景成功
    }


    /// 
    /// 由于需要两次调用,在这里进行简单封装
    /// 
    /// 等一帧
    private IEnumerator LoadProgress()
    {
        while (currentProgress < targetProgress) //当前进度 < 目标进度时
        {
            //++currentProgress;
            currentProgress+=5;
            //当前进度不断累加 (Chinar温馨提示,如果场景很小,可以调整这里的值 例如:+=10 +=20,来调节加载速度)
            slider.value = (float)currentProgress / 100; //给UI进度条赋值
            yield return new WaitForEndOfFrame();         //等一帧
        }
    }
}

你可能感兴趣的:(unity)