当前位置:   article > 正文

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

unity 加载场景 事件

说明:
  这里通过 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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/125260
推荐阅读
相关标签
  

闽ICP备14008679号