其他分享
首页 > 其他分享> > Unity 根据进度条加载切换场景

Unity 根据进度条加载切换场景

作者:互联网

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

public class LoadSprite : MonoBehaviour
{


    public Image jindu;
    //异步加载场景
    public AsyncOperation async;
    bool isLoad;
    void Start()
    {
        //开启协同程序
        StartCoroutine(loadScene());
    }
    IEnumerator loadScene()
    {//尝试加载场景
        try
        {//加载场景
            async = SceneManager.LoadSceneAsync("Update_");//LoadSceneAsyne("scene2");
            //加载完后不跳转
            async.allowSceneActivation = false;//allowSceneAcivation = false;
        }
        catch (Exception er)
        {//输出错误信息
            Debug.Log(er);
        }
        while (!async.isDone && async.progress < 0.9f)
        { yield return null; }
        //加载完了
        isLoad = true;
    }

    void Update()
    {
        if (jindu.fillAmount < 0.9f) 
        {
            //进度条.value = async.progress;
            jindu.fillAmount += UnityEngine.Random.Range(0.0009f, 0.005f);
        }
        else if (isLoad == true)
        {
            jindu.fillAmount += UnityEngine.Random.Range(0.0009f, 0.005f);
            if (jindu.fillAmount >= 1)
            {//场景跳转
                async.allowSceneActivation = true;
            }
        }
    }
}


标签:UnityEngine,进度条,Unity,fillAmount,using,async,jindu,加载
来源: https://blog.csdn.net/qq_39231744/article/details/120159059