Unity异步加载场景,加载到100%后按任意键进入主场景

本文介绍了如何使用Unity3D的LoadSceneAsync进行场景加载,并配合 PlayerPrefs设置场景索引,同时实现了一个平滑的加载进度条和交互提示。通过脚本展示了如何控制加载过程中的UI更新和动画效果。
摘要由CSDN通过智能技术生成

说明:
  这里通过 sceneIndex= PlayerPrefs.GetInt("SceneIndex"); 来确定要加载的场景索引。 根据需求自己设定PlayerPrefs.SetInt("SceneIndex"); 即可

  直接赋值将对应组件拖拽上即可
在这里插入图片描述
脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
using QFramework;

//场景的异步加载
public class LoadSceneAsyncController : MonoBehaviour
{
    [Header("场景加载进度条:")]
    public Slider scene_Slider;
    [Header("进度条显示:")]
    public Text process_Txt;
    [Header("进度条提示信息:")]
    public Text tips_Txt;

    AsyncOperation _async;
    int sceneIndex=0;

    //是否可以进入场景
    private bool isCanIntoScene = false;

    void Start()
    {
        scene_Slider.value = 0;
        tips_Txt.text = "资源加载中...";
        isCanIntoScene = false;
    }

     void OnEnable()
    {
        StartCoroutine(LoadAsync_IE(sceneIndex));
    }

    void Update()
    {
       
        if (Input.anyKeyDown&&isCanIntoScene)
        {
            _async.allowSceneActivation = true;
            isCanIntoScene = false;
        }
    }
    
    IEnumerator LoadAsync_IE(int sceneIndex)
    {
        sceneIndex= PlayerPrefs.GetInt("SceneIndex");
        Debug.Log("当前场景索引为:" + sceneIndex);
        yield return new WaitForSeconds(0.1f);
        Debug.Log("当前的场景索引为:" + sceneIndex);
        _async = SceneManager.LoadSceneAsync(sceneIndex);
        float nowProgress = 0;
        float endProgress = 0;
        _async.allowSceneActivation = false;

        while (_async.progress < 0.9f)
        {
            endProgress = _async.progress * 100f;

            while (nowProgress < endProgress)
            {
                ++nowProgress;
                scene_Slider.value = nowProgress / 100;
                process_Txt.text = (int)(nowProgress) + "%";
                yield return new WaitForEndOfFrame();
            }
        }

        endProgress = 100;
        while (nowProgress < endProgress)
        {
            ++nowProgress;
            scene_Slider.value = nowProgress / 100;
            process_Txt.text = (int)(nowProgress) + "%";
            yield return new WaitForEndOfFrame();
        }
        
        isCanIntoScene = true;
        //tips_Txt.gameObject.SetActive(true);
        tips_Txt.text = "按下任意键继续...";
        
        tips_Txt.DOFade(1, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(0, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(1, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(0, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(1, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(0, 0.5f);
        yield return new WaitForSeconds(0.5f);
        tips_Txt.DOFade(1, 0.5f);
    }
}

加载进度条:
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值