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

说明:
  这里通过 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);
    }
}

转载https://blog.csdn.net/weixin_44446603/article/details/119487151?spm=1001.2014.3001.5502

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,你可以使用异步加载场景的方法来避免在切换场景时的卡顿或停顿。下面是一个示例代码,展示了如何使用异步加载场景: ```csharp using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; public class SceneLoader : MonoBehaviour { public string sceneName; // 要加载场景名字 public void LoadSceneAsync() { StartCoroutine(LoadSceneAsyncCoroutine()); } IEnumerator LoadSceneAsyncCoroutine() { // 异步加载场景 AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName); // 等待场景加载完成 while (!asyncLoad.isDone) { // 可以在这里显示加载进度或执行其他操作 float progress = Mathf.Clamp01(asyncLoad.progress / 0.9f); // 异步加载的进度范围为0-0.9 Debug.Log("Loading progress: " + (progress * 100) + "%"); yield return null; } } } ``` 在上述代码中,我们首先定义了一个`LoadSceneAsync`方法来启动异步加载场景的过程。然后,在`LoadSceneAsyncCoroutine`协程中,我们使用`SceneManager.LoadSceneAsync`方法来异步加载指定的场景。 使用`AsyncOperation`对象可以获取加载进度,将其归一化为0-1的范围,并在需要时进行显示或其他操作。注意,在异步加载的过程中,进度会达到0.9,因此我们使用`Mathf.Clamp01`来将进度限制在0-0.9之间。 最后,使用`yield return null`来等待一帧的时间,然后再次循环检查加载进度。当加载进度达到1时,表示场景加载完成。 你可以在需要异步加载场景的地方调用`LoadSceneAsync`方法,这样就可以在切换场景时避免卡顿或停顿。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值