Unity3D 场景切换异步加载进度

异步加载场景分为A、B、C三个场景

A场景是开始场景;B场景是加载场景(进度条加载显示);C场景是目标场景

在A场景中添加一个按钮,触发函数:

//异步加载新场景  
public void LoadNewScene()  
{  
    //保存需要加载的目标场景  
    Globe.nextSceneName = "Scene";  
  
    SceneManager.LoadScene("Loading");        
}  



在B场景中添加一个脚本(AsyncLoadScene),挂载到Camera下

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

public class Globe
{
    public static string nextSceneName;

    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="sceneName">场景名字</param>
    /// <param name="isAsync">是否使用异步加载</param>
    public static void LodingScene(string sceneName, bool isAsync = true)
    {
        //清理Unity垃圾
        Resources.UnloadUnusedAssets();
        //清理GC垃圾
        System.GC.Collect();
        //是否使用异步加载
        if (isAsync)
        {
            //赋值加载场景名称
            nextSceneName = sceneName;
            //跳转到LoadingScene场景
            SceneManager.LoadScene("LoadingScene");
        }
        else
        {
            SceneManager.LoadScene(sceneName);
        }
    }
    
}
/// <summary>
/// 异步加载脚本
/// </summary>
public class AsyncLoadScene : MonoBehaviour
{
    public Slider loadingSlider;

    //public Text Tips;
    //public Text loadingText;

    //private float loadingSpeed = 1;

    //private float targetValue;

    private AsyncOperation operation;

    private int displayProgress;
    private int toProgress;

    void Start()
    {
        loadingSlider.value = 0;
        loadingSlider.maxValue = 100;

        if (SceneManager.GetActiveScene().name == "LoadingScene")
        {
            //启动协程  
            StartCoroutine(AsyncLoading());
        }
    }
    private IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync(Globe.nextSceneName);
        operation.allowSceneActivation = false;
        while (operation.progress < 0.9f)
        {
            toProgress = (int)operation.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }
        }
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        operation.allowSceneActivation = true;
    }

    private void SetLoadingPercentage(int Percentage)
    {
        loadingSlider.value = Percentage;
        //loadingText.text = Percentage + "%";
    }

  
}



这里需要注意的是使用AsyncOperation.allowSceneActivation属性来对异步加载的场景进行控制

为true时,异步加载完毕后将自动切换到C场景

最后附上效果图




  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值