NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/SubSystems/GameSceneManager.cs

164 lines
5.3 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using Cysharp.Threading.Tasks;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2024-02-21 17:54:08 +08:00
using PhxhSDK.Res;
2024-08-16 13:55:00 +08:00
using System.Collections.Generic;
2024-03-08 12:08:21 +08:00
using UnityEngine;
2023-08-22 19:08:45 +08:00
using UnityEngine.SceneManagement;
namespace Gameplay.SubSystems
{
2024-01-15 15:38:20 +08:00
/// <summary>
/// 游戏场景的底层逻辑
/// </summary>
2023-10-19 15:00:19 +08:00
public class GameSceneManager : Singlenton<GameSceneManager>
2023-08-22 19:08:45 +08:00
{
2024-08-16 13:55:00 +08:00
/// <summary>
/// 空场景名
/// </summary>
private const string EMPTY_SCENE = "Empty.unity";
/// <summary>
/// 空场景路径
/// </summary>
private readonly string EMPTY_SCENE_PATH = $"Assets/Scenes/{EMPTY_SCENE}";
/// <summary>
/// 是否使用低品质场景
/// </summary>
private bool useLowScene;
2024-08-15 19:37:01 +08:00
/// <summary>
/// 场景加载器
/// </summary>
2024-08-16 13:55:00 +08:00
private readonly ISceneLoader sceneLoader;
/// <summary>
/// 加载中的场景列表
/// </summary>
private readonly HashSet<string> setLoadingScene = new();
/// <summary>
/// 等待卸载的场景
/// </summary>
private string waitUnloadScene;
2024-08-15 19:37:01 +08:00
2024-02-21 17:54:08 +08:00
public GameSceneManager()
2023-12-29 19:58:17 +08:00
{
2024-03-08 12:08:21 +08:00
#if UNITY_EDITOR
if (!Application.isPlaying)
{
2024-08-16 13:55:00 +08:00
sceneLoader = new SceneLoader_Fake();
2024-03-08 12:08:21 +08:00
return;
}
#endif
2024-02-21 17:54:08 +08:00
#if USE_YOO
2024-08-16 13:55:00 +08:00
sceneLoader = new SceneLoader_YooAsset();
2024-02-21 17:54:08 +08:00
#else
_sceneLoader = new SceneLoader_Addressables();
#endif
2023-12-29 19:58:17 +08:00
}
2024-08-15 19:37:01 +08:00
public void SetSceneLoadingSetting(bool isUseLowScene)
{
2024-08-16 13:55:00 +08:00
useLowScene = isUseLowScene;
2024-08-15 19:37:01 +08:00
}
2024-08-16 14:47:06 +08:00
public async UniTask<SceneHandle> LoadSceneAsync(string scenePath, LoadSceneMode mode = LoadSceneMode.Single)
2023-10-19 15:00:19 +08:00
{
2024-08-16 13:55:00 +08:00
string realScenePath = GetPostProcessedScenePath(scenePath); // 真实加载路径
if (string.IsNullOrEmpty(realScenePath))
{
DebugUtil.LogError($"转换后的场景路径为空!无法加载");
return null;
}
if (!setLoadingScene.Add(realScenePath))
{
DebugUtil.LogError($"上一个相同场景还在加载中,请等待加载完成后再次加载:{realScenePath}");
return null;
}
PhxhSDK.Res.SceneHandle sceneHandle = null;
if (!sceneLoader.IsExistScene(realScenePath))
{
if (LoadSceneMode.Single == mode)
{
// 先切换到Empty场景
await sceneLoader.LoadSceneAsync(EMPTY_SCENE_PATH, true);
await Resources.UnloadUnusedAssets();
}
sceneHandle = await sceneLoader.LoadSceneAsync(realScenePath, false, mode);
}
setLoadingScene.Remove(realScenePath);
if (!string.IsNullOrEmpty(waitUnloadScene))
{
if (scenePath == waitUnloadScene)
{
DebugUtil.Log($"等待卸载的场景和当前加载的场景一样,不卸载:{waitUnloadScene}");
waitUnloadScene = null;
}
else
{
DebugUtil.Log($"卸载之前的场景:{waitUnloadScene}");
await UnLoadSceneAsync(waitUnloadScene);
waitUnloadScene = null;
}
}
if (mode == LoadSceneMode.Single)
await sceneLoader.UnAllLoadSceneAsync(realScenePath);
return sceneHandle;
2023-11-21 15:04:56 +08:00
}
public async UniTask UnLoadSceneAsync(string scenePath)
{
2024-08-27 14:03:58 +08:00
string realScenePath = GetPostProcessedScenePath(scenePath);
2024-08-16 13:55:00 +08:00
2024-08-27 14:03:58 +08:00
if (string.IsNullOrEmpty(realScenePath))
2024-08-16 13:55:00 +08:00
{
2024-08-27 14:03:58 +08:00
DebugUtil.LogError($"卸载失败,路径:{realScenePath}");
2024-08-16 13:55:00 +08:00
return;
}
2024-08-27 14:03:58 +08:00
await sceneLoader.UnLoadSceneAsync(realScenePath);
if (null != scenePath && string.Empty != scenePath)
waitUnloadScene = scenePath;
2023-10-19 15:00:19 +08:00
}
2023-12-29 15:07:04 +08:00
/// <summary>
/// 返回场景加载的进度
/// </summary>
/// <param name="scenePath"></param>
/// <returns>0-100的浮点数</returns>
2023-12-29 15:07:04 +08:00
public float GetSceneLoadingProgress(string scenePath)
{
2024-08-16 13:55:00 +08:00
return sceneLoader.GetSceneLoadingProgress(GetPostProcessedScenePath(scenePath));
2023-12-29 15:07:04 +08:00
}
2024-01-26 17:14:03 +08:00
2024-08-16 13:55:00 +08:00
/// <summary>
/// 获取后处理场景路径
/// </summary>
/// <param name="scenePath"></param>
/// <returns></returns>
private string GetPostProcessedScenePath(string scenePath)
{
if (scenePath.Equals(EMPTY_SCENE))
return scenePath;
if (useLowScene && !scenePath.Contains("_low"))
{
string lowScenePath = scenePath.Replace(".unity", "_low.unity");
if (PostProcessData.Instance.TryGetPostProcessedPath(lowScenePath, out string finalLowScenePath))
return finalLowScenePath;
}
bool isFind = PostProcessData.Instance.TryGetPostProcessedPath(scenePath, out string finalScenePath);
if (!isFind)
DebugUtil.LogError($"未找到场景的后处理:{scenePath}");
return finalScenePath;
}
2023-08-22 19:08:45 +08:00
}
}