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

95 lines
3.0 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System.Collections;
2023-11-21 15:04:56 +08:00
using System.Collections.Generic;
using System.Linq;
2023-08-22 19:08:45 +08:00
using Cysharp.Threading.Tasks;
using Framework;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
2023-10-19 15:00:19 +08:00
using UnityEngine.AddressableAssets;
2023-11-21 15:04:56 +08:00
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
2023-08-22 19:08:45 +08:00
using UnityEngine.SceneManagement;
namespace Gameplay.SubSystems
{
2023-10-19 15:00:19 +08:00
public class GameSceneManager : Singlenton<GameSceneManager>
2023-08-22 19:08:45 +08:00
{
public enum EnumScene
{
GameStart, // 登录场景
GameMain, // 主场景
GameLevel // 关卡(战场)场景
}
2023-10-19 15:00:19 +08:00
public const string SCENE_PREFIX = "Assets/Scenes/";
2023-08-22 19:08:45 +08:00
public const string START_SCENE_NAME = "StartScene";
public const string LEVEL_SCENE_NAME = "LevelScene";
public const string MAIN_SCENE_NAME = "MainScene";
2023-10-19 15:00:19 +08:00
public const string SCENE_SUFFIX = ".unity";
2023-08-22 19:08:45 +08:00
2023-11-21 15:04:56 +08:00
private Dictionary<string, AsyncOperationHandle<SceneInstance>> _sceneOpDic = new();
2023-08-22 19:08:45 +08:00
public async UniTask ChangeScene(EnumScene enumScene)
{
2023-10-19 15:00:19 +08:00
string sceneName = SCENE_PREFIX;
2023-08-22 19:08:45 +08:00
switch (enumScene)
{
case EnumScene.GameStart:
2023-10-19 15:00:19 +08:00
sceneName += START_SCENE_NAME;
2023-08-22 19:08:45 +08:00
break;
case EnumScene.GameMain:
2023-10-19 15:00:19 +08:00
sceneName += MAIN_SCENE_NAME;
2023-08-22 19:08:45 +08:00
break;
case EnumScene.GameLevel:
2023-10-19 15:00:19 +08:00
sceneName += LEVEL_SCENE_NAME;
2023-08-22 19:08:45 +08:00
break;
default:
2023-11-21 15:04:56 +08:00
DebugUtil.LogError("{0}.ChangeScene, {1} has no scene name matched", GetType(), enumScene);
2023-10-19 15:00:19 +08:00
sceneName += START_SCENE_NAME;
2023-08-22 19:08:45 +08:00
break;
}
2023-11-21 15:04:56 +08:00
2023-10-19 15:00:19 +08:00
sceneName += SCENE_SUFFIX;
2023-11-22 15:05:43 +08:00
await LoadSceneAsync(sceneName);
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
public async UniTask LoadSceneAsync(string scenePath, LoadSceneMode mode = LoadSceneMode.Single)
{
2023-11-20 14:10:18 +08:00
if (string.IsNullOrEmpty(scenePath))
{
DebugUtil.LogError($"ScenePath is null!");
return;
}
2023-11-21 15:04:56 +08:00
2023-11-22 15:05:43 +08:00
if(mode == LoadSceneMode.Single)
_sceneOpDic.Clear();
2023-11-21 15:04:56 +08:00
if (!_sceneOpDic.TryGetValue(scenePath, out var op))
{
op = Addressables.LoadSceneAsync(scenePath, mode);
_sceneOpDic.Add(scenePath, op);
}
await op.ToUniTask();
}
public async UniTask UnLoadSceneAsync(string scenePath)
{
if (string.IsNullOrEmpty(scenePath))
{
DebugUtil.LogError($"ScenePath is null!");
return;
}
if (!_sceneOpDic.TryGetValue(scenePath, out var op))
{
Debug.LogError($"该场景没有被加载过 scenePath:{scenePath}");
return;
}
_sceneOpDic.Remove(scenePath);
await Addressables.UnloadSceneAsync(op).ToUniTask();
2023-10-19 15:00:19 +08:00
}
2023-08-22 19:08:45 +08:00
}
}