我的Unity工具类---异步加载

↓不带进度条的异步加载

	/// <summary>
    /// 异步加载
    /// </summary>
    /// <returns></returns>
    public static IEnumerator LoadAsyncScene(string sceneName, Action action = null)
    {
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
        if (action != null)
        {
            action();
        }
    }

↓协程更新进度条, WebGL用这个会报错 (PS:现在回头看看, 当时写的代码实在太蠢了…)

	/// <summary>
    /// 异步加载带进度条
    /// </summary>
    /// <returns></returns>
    public static IEnumerator LoadAsyncScene(string sceneName, Action action = null)
    {
        //进度
        int nowProgress = 0;
        int toProgress = 0;
        //进度条
        Slider slider = GameObject.Find("Canvas").transform.Find("Bg/Slider").GetComponent<Slider>();
        //进度文字
        Text loadText = GameObject.Find("Canvas").transform.Find("Bg/Text").GetComponent<Text>();
        //图片: loading
        Transform image = GameObject.Find("Canvas").transform.Find("Bg/Image");
        //初始化
        slider.value = 0;
        loadText.text = "0%";
        slider.gameObject.SetActive(true);
        loadText.gameObject.SetActive(true);
        image.gameObject.SetActive(true);
        //加载场景
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
        //先不激活场景, 这时候Unity加载到90%就自己停
        asyncLoad.allowSceneActivation = false;
        //90%之前按加载进步跑
        while (asyncLoad.progress < 0.9f)
        {
            toProgress = (int)asyncLoad.progress * 100;
            while (nowProgress < toProgress)
            {
                ++nowProgress;
                loadText.text = nowProgress + "%";
                slider.value = nowProgress / 100f;
                yield return new WaitForEndOfFrame();
            }
        }
        //跑到100%
        toProgress = 100;
        while (nowProgress < toProgress)
        {
            ++nowProgress;
            loadText.text = nowProgress + "%";
            slider.value = nowProgress / 100f;
            yield return new WaitForEndOfFrame();
        }
        //激活场景, Unity加载到100%
        asyncLoad.allowSceneActivation = true;
        //UI隐藏掉
        slider.gameObject.SetActive(false);
        loadText.gameObject.SetActive(false);
        image.gameObject.SetActive(false);
        //等一帧再加载方法, 不然可能方法执行不了而报空
        yield return null;
        if (action != null)
        {
            action();
        }
    }

↓在Update里更新进度条, WebGL能用

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

public class LoadUpdate : MonoBehaviour
{
    public static LoadUpdate instance;

    //需要挂上的
    [Header("加载页面")]
    public GameObject panelLoad;
    [Header("加载的进度条")]
    public Slider loadSlider;
    [Header("加载进度的显示文字")]
    public Text loadText;

    private int loadVaule;
    private int currentVaule;
    private AsyncOperation asyncOperation;

    private void Awake()
    {
        if (instance == null )
        {
            instance = this;
        }
        else if (instance !=this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
    }

    /// <summary>
    /// 调用加载
    /// </summary>
    public void LoadScene(string sceneName, Action action = null)
    {
        StartCoroutine(LoadSceneAsync(sceneName, action));
    }

    /// <summary>
    /// 异步加载场景,加载完成后执行Action
    /// </summary>
    private IEnumerator LoadSceneAsync(string sceneName, Action action = null)
    {
        if (panelLoad != null)
        {
            panelLoad.SetActive(true);
        } 
        asyncOperation = SceneManager.LoadSceneAsync(sceneName);
        asyncOperation.allowSceneActivation = false;
        yield return asyncOperation;

        if (panelLoad != null)
        {
            panelLoad.SetActive(false);
        }

        if (action != null)
        {
            action.Invoke();
        }
    }

    /// <summary>
    /// 因为更新进度条的方法放进协程里打包WebGl会卡住,所以放进Updata里
    /// </summary>
    private void Update()
    {
        if (asyncOperation == null)
        {
            return;
        }

        //在90%之前正常加载
        if (asyncOperation.progress < 0.9f)
        {
            loadVaule = (int)asyncOperation.progress * 100;
        }
        //到90%的时候就设为100%
        else
        {
            loadVaule = 100;
        }
        //更新进度
        if (currentVaule < loadVaule)
        {
            currentVaule++;
        }
        //加载完了
        if (currentVaule >= 100)
        {
            asyncOperation.allowSceneActivation = true;
        }

        //更新Slider和Text
        if (loadText != null)
        {
            loadText.text = currentVaule + "%";
        }
        if (loadSlider != null)
        {
            loadSlider.value = currentVaule / 100f;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值