Unity 3D开发--SceneManager场景管理(异步使用同一个过渡场景)

在U3D开发过程中经常使用到多场景的切换,有同步SceneManager.LoadScene()和异步SceneManager.LoadSceneAsync()两种方法,同步的话一般就会卡住界面直到加载完成,使用异步的话一般都做一个加载的进度条,每次切换的时候都需要一个加载动画,所以需要建一个专门的过渡加载场景来进行统一加载,也可以避免场景直接切换出现的黑屏。

一、建立一个单例进行切换,在项目代码的任何位置都可以调用


public class LoadSceneManager 
{
    private static LoadSceneManager instance;
    public static LoadSceneManager Instance
    {
        get {
            if (instance == null)
            {
                instance = new LoadSceneManager();
            }
            return instance;
        }
    }

    public string nextSceneName;

    //异步加载
    public void LoadSceneAsync(string sceneName)
    {
        Debug.Log("LoadSceneAsync:" + sceneName);
        nextSceneName = sceneName;
        SceneManager.LoadScene("LoadingScene");
    }
    public void LoadScene(string sceneName)
    {
        Debug.Log("LoadScene:" + sceneName);
        SceneManager.LoadScene(sceneName);
    }

}

使用的时候直接使用LoadSceneManager.Instance.LoadSceneAsync("SecondScene");

二、异步加载的过程

要做个进度条然后进行管理,代码很简单就不说了


public class LoadingController : MonoBehaviour
{
    public Slider loadingSlider;
    public TMP_Text loadingText;
    private AsyncOperation asyncOperation;
    private float operationProgress;

    // Start is called before the first frame update
    void Start()
    {
        loadingSlider.value = 0.0f;
        if (SceneManager.GetActiveScene().name == "LoadingScene")
        {
            StartCoroutine("LoadingScene");
        }
    }

    IEnumerator LoadingScene()
    {
        asyncOperation = SceneManager.LoadSceneAsync(LoadSceneManager.Instance.nextSceneName);
        asyncOperation.allowSceneActivation = false;
        yield return asyncOperation;
    }

    // Update is called once per frame
    void Update()
    {
        operationProgress = asyncOperation.progress;
        //最大值只到0.9,后面有进行插值运算更新
        if (Mathf.Approximately(operationProgress, 0.9f))
        {
            operationProgress = 1;
        }
        if (Mathf.Approximately(operationProgress, 0.6f))
        {
            System.Threading.Thread.Sleep(5000);
        }
        UpdateLoadingUI(operationProgress);
    }
    private void UpdateLoadingUI(float value)
    {
        if (operationProgress != loadingSlider.value)
        {
            loadingSlider.value = Mathf.Lerp(loadingSlider.value, operationProgress, Time.deltaTime * 2);
            if (Mathf.Approximately(operationProgress, operationProgress))
            {
                loadingSlider.value = operationProgress;
            }
        }
        Debug.Log("Progress:" + loadingSlider.value);
        if (loadingText != null)
        {
            loadingText.text = Mathf.Round(loadingSlider.value * 100) + "%";
        }

        //自动换场景
        if (Mathf.Approximately(loadingSlider.value, 1.0f))
        {
            asyncOperation.allowSceneActivation = true;
        }
    }
}

三、demo链接

https://download.csdn.net/download/emailforwei/87329335


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值