微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐