Unity场景的异步加载

本文介绍了如何在Unity中实现异步场景加载,包括从起始场景A通过点击事件过渡到中间场景B,并在B场景显示进度条,直至加载完成进入目标场景C。关键步骤包括设置场景、编写AsyncLoadScene核心脚本、处理Cube的点击事件及进度条布局调整。
摘要由CSDN通过智能技术生成

第一步:创建三个场景,分别为 A B C

第二步:shift+ctrl+b 分别把三个场景加载进去

第三步:开始干活

场景 A 是开始场景

场景B 是中间过渡场景

场景C 是最终需要到达(加载)的场景

准备好脚本:

我没有用button,而是用的gameobject作为点击事件 所以就有一点不同

AsyncLoadScene(此脚本是核心内容)

CubeEventItemBind(此脚本是cube的点击事件)

AddEeventMonoTest(此脚本是cube添加点击事件)

直接上


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

public class Globe
{
    public static string nextSceneName ;
}

public class AsyncLoadScene : MonoBehaviour
{
    public Slider loadingSlider;

    public Text loadingText;

    private float loadingSpeed = 1;

    private float targetValue;

    private AsyncOperation operation;

    // Use this for initialization
    void Start()
    {
        loadingSlider.value = 0.0f;

        if (SceneManager.GetActiveScene().name == "B")
        {
            //启动协程
            StartCoroutine(AsyncLoading());
        }
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync("Scenen");
        //阻止当加载完成自动切换
        operation.allowSceneActivation = false;

        yield return operation;
    }

    // Update is called once per frame
    void Update()
    {
        targetValue = operation.progress;

        if (operation.progress >= 0.9f)
        {
            //operation.progress的值最大为0.9
            targetVa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值