loudixvmuniu/Assets/_Scripts/Framework/Utilities/LoadScenesAsyncNoPanel.cs
shenjianxing 40de877779 适配VR
2025-04-02 14:37:35 +08:00

76 lines
2.7 KiB
C#

using CG.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/*******************************************************************************
*Create By CG
*Function 异步场景加载控制(无界面显示)
*******************************************************************************/
namespace CG.UTility
{
public class LoadScenesAsyncNoPanel : MonoSingleton<LoadScenesAsyncNoPanel>
{
private bool _isLoadScene;//是否加载场景
private AsyncOperation _async;
private int _toProgress = 0;//读取场景进度
private int _nowProgress = 0;//当前场景返回进度
private System.Action _loadedCall = null;
public GameObject eventsystem;
private void Update()
{
if (_isLoadScene)
{
if (_async == null) return;
if (_async.progress < 0.9f)
{
_toProgress = (int)_async.progress * 100;
}
else
{
_toProgress = 100;
}
if (_nowProgress < _toProgress)
{
_nowProgress++;
}
if (_nowProgress == 100)
{
if (_async.isDone)
{
_loadedCall?.Invoke();
_isLoadScene = false;
StopCoroutine("LoadScene");
UIRoot.Instance.GetEventSystem().gameObject.SetActive(true);
Framework.UI_Manage.Instance.ClosePanel("LoadingSceenPanel");
}
_async.allowSceneActivation = true;
}
}
}
/// <summary>
/// 加载场景主方法
/// </summary>
/// <param name="sceneName"></param>
public void LoadSceneAsync(string sceneName, System.Action LoadedCall)
{
_loadedCall = LoadedCall;
_toProgress = 0;
_nowProgress = 0;
_isLoadScene = true;
StartCoroutine("LoadScene", sceneName);
Framework.UI_Manage.Instance.ShowPanel("LoadingSceenPanel", System.Type.GetType("CG.Framework.LoadingSceenPanel"), Framework.UIGroup.Tip);
UIRoot.Instance.GetEventSystem().gameObject.SetActive(false);
}
private IEnumerator LoadScene(string sceneName)
{
_async = SceneManager.LoadSceneAsync(sceneName);
_async.allowSceneActivation = false;
yield return new WaitForEndOfFrame();//加上这句可以先让加载画面显示
yield return _async;
}
}
}