2025-07-08 10:46:31 +00:00
|
|
|
using System;
|
2025-07-10 10:18:07 +00:00
|
|
|
using System.Collections.Generic;
|
2025-07-08 10:46:31 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using UnityEngine;
|
2025-07-10 10:18:07 +00:00
|
|
|
using UnityEngine.ResourceManagement.ResourceProviders;
|
2025-07-08 10:46:31 +00:00
|
|
|
|
|
|
|
namespace DDD
|
|
|
|
{
|
|
|
|
public enum SceneType
|
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
Entry = 0,
|
2025-07-08 10:46:31 +00:00
|
|
|
Restaurant = 1,
|
|
|
|
Voyage = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SceneManager : Singleton<SceneManager>, IManager
|
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
private Dictionary<SceneType, SceneInstance> _loadedScenes;
|
|
|
|
|
2025-07-10 10:32:01 +00:00
|
|
|
private SceneInstance _currentSceneInstance;
|
|
|
|
|
|
|
|
public Action<SceneInstance> OnSceneChanged;
|
2025-07-09 09:45:11 +00:00
|
|
|
|
2025-07-08 10:46:31 +00:00
|
|
|
public void Init()
|
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
Array sceneTypeArray = Enum.GetValues(typeof(SceneType));
|
|
|
|
_loadedScenes = new Dictionary<SceneType, SceneInstance>(sceneTypeArray.Length);
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
public async void PostInit()
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
foreach (SceneType sceneType in Enum.GetValues(typeof(SceneType)))
|
|
|
|
{
|
|
|
|
if (sceneType == SceneType.Entry) continue;
|
|
|
|
|
|
|
|
var sceneInstance = await AssetManager.LoadScene(sceneType.ToString());
|
|
|
|
if (sceneInstance.Scene.IsValid())
|
|
|
|
{
|
|
|
|
_loadedScenes[sceneType] = sceneInstance;
|
|
|
|
|
|
|
|
foreach (var go in sceneInstance.Scene.GetRootGameObjects())
|
|
|
|
{
|
|
|
|
go.SetActive(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Debug.LogWarning($"Scene preload failed\n{e}");
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
public SceneInstance GetSceneInstance(SceneType sceneType) => _loadedScenes[sceneType];
|
2025-07-08 10:46:31 +00:00
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
public async Task PreloadSceneAsync(SceneType sceneType)
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
if (_loadedScenes.ContainsKey(sceneType))
|
2025-07-08 10:46:31 +00:00
|
|
|
return;
|
2025-07-10 10:18:07 +00:00
|
|
|
|
|
|
|
string key = sceneType.ToString();
|
|
|
|
SceneInstance sceneInstance = await AssetManager.LoadScene(key);
|
2025-07-08 10:46:31 +00:00
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
if (sceneInstance.Scene.IsValid())
|
|
|
|
{
|
|
|
|
_loadedScenes[sceneType] = sceneInstance;
|
2025-07-08 10:46:31 +00:00
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
foreach (var root in sceneInstance.Scene.GetRootGameObjects())
|
|
|
|
{
|
|
|
|
root.SetActive(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.LogError($"[SceneManager] Failed to preload scene: {sceneType}");
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
public void ActivateScene(SceneType sceneType)
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
if (_loadedScenes.TryGetValue(sceneType, out var sceneInstance))
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
foreach (var root in sceneInstance.Scene.GetRootGameObjects())
|
|
|
|
{
|
|
|
|
root.SetActive(true);
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
UnityEngine.SceneManagement.SceneManager.SetActiveScene(sceneInstance.Scene);
|
2025-07-10 10:32:01 +00:00
|
|
|
_currentSceneInstance = sceneInstance;
|
2025-07-10 10:18:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.LogError($"[SceneManager] Scene not loaded: {sceneType}");
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
|
2025-07-10 10:18:07 +00:00
|
|
|
public void DeactivateScene(SceneType sceneType)
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
if (_loadedScenes.TryGetValue(sceneType, out var sceneInstance))
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
foreach (var root in sceneInstance.Scene.GetRootGameObjects())
|
|
|
|
{
|
|
|
|
root.SetActive(false);
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
2025-07-10 10:18:07 +00:00
|
|
|
|
|
|
|
public async Task UnloadSceneAsync(SceneType sceneType)
|
2025-07-08 10:46:31 +00:00
|
|
|
{
|
2025-07-10 10:18:07 +00:00
|
|
|
if (_loadedScenes.TryGetValue(sceneType, out var sceneInstance))
|
|
|
|
{
|
|
|
|
await AssetManager.UnloadScene(GetSceneInstance(sceneType));
|
|
|
|
}
|
2025-07-08 10:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|