From 52ac77d5e41f32117a58ac72200f6e17c50d8111 Mon Sep 17 00:00:00 2001 From: Alex-Rachel <574809918@qq.com> Date: Tue, 18 Mar 2025 14:39:08 +0800 Subject: [PATCH] =?UTF-8?q?SceneModule=E5=A2=9E=E5=8A=A0UniTask=E7=9A=84Lo?= =?UTF-8?q?adScene=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Module/SceneModule/ISceneModule.cs | 15 +++- .../Runtime/Module/SceneModule/SceneModule.cs | 78 +++++++++++++++++-- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/ISceneModule.cs b/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/ISceneModule.cs index 9bc84168..26ab0dff 100644 --- a/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/ISceneModule.cs +++ b/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/ISceneModule.cs @@ -1,4 +1,5 @@ using System; +using Cysharp.Threading.Tasks; using UnityEngine.SceneManagement; namespace TEngine @@ -10,6 +11,18 @@ namespace TEngine /// public string CurrentMainSceneName { get; } + /// + /// 加载场景。 + /// + /// 场景的定位地址 + /// 场景加载模式 + /// 加载完毕时是否主动挂起 + /// 优先级 + /// 加载主场景是否回收垃圾。 + /// 加载进度回调。 + public UniTask LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, bool gcCollect = true, + Action progressCallBack = null); + /// /// 加载场景。 /// @@ -20,7 +33,7 @@ namespace TEngine /// 加载回调。 /// 加载主场景是否回收垃圾。 /// 加载进度回调。 - public Scene LoadScene(string location, + public void LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, diff --git a/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/SceneModule.cs b/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/SceneModule.cs index 8e148853..8bd6b93d 100644 --- a/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/SceneModule.cs +++ b/UnityProject/Assets/TEngine/Runtime/Module/SceneModule/SceneModule.cs @@ -42,6 +42,74 @@ namespace TEngine _currentMainSceneName = string.Empty; } + /// + /// 加载场景。 + /// + /// 场景的定位地址 + /// 场景加载模式 + /// 加载完毕时是否主动挂起 + /// 优先级 + /// 加载主场景是否回收垃圾。 + /// 加载进度回调。 + public async UniTask LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, bool gcCollect = true, Action progressCallBack = null) + { + if (sceneMode == LoadSceneMode.Additive) + { + if (_subScenes.TryGetValue(location, out SceneHandle subScene)) + { + throw new Exception($"Could not load subScene while already loaded. Scene: {location}"); + } + + subScene = YooAssets.LoadSceneAsync(location, sceneMode, LocalPhysicsMode.None, suspendLoad, priority); + + + if (progressCallBack != null) + { + while (!subScene.IsDone && subScene.IsValid) + { + progressCallBack.Invoke(subScene.Progress); + await UniTask.Yield(); + } + } + else + { + await subScene.ToUniTask(); + } + + _subScenes.Add(location, subScene); + + return subScene.SceneObject; + } + else + { + if (_currentMainScene is { IsDone: false }) + { + throw new Exception($"Could not load MainScene while loading. CurrentMainScene: {_currentMainSceneName}."); + } + + _currentMainSceneName = location; + + _currentMainScene = YooAssets.LoadSceneAsync(location, sceneMode, LocalPhysicsMode.None, suspendLoad, priority); + + if (progressCallBack != null) + { + while (!_currentMainScene.IsDone && _currentMainScene.IsValid) + { + progressCallBack.Invoke(_currentMainScene.Progress); + await UniTask.Yield(); + } + } + else + { + await _currentMainScene.ToUniTask(); + } + + ModuleSystem.GetModule().ForceUnloadUnusedAssets(gcCollect); + + return _currentMainScene.SceneObject; + } + } + /// /// 加载场景。 /// @@ -52,7 +120,7 @@ namespace TEngine /// 加载回调。 /// 加载主场景是否回收垃圾。 /// 加载进度回调。 - public Scene LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, + public void LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, Action callBack = null, bool gcCollect = true, Action progressCallBack = null) { @@ -61,7 +129,7 @@ namespace TEngine if (_subScenes.TryGetValue(location, out SceneHandle subScene)) { Log.Warning($"Could not load subScene while already loaded. Scene: {location}"); - return default; + return; } subScene = YooAssets.LoadSceneAsync(location, sceneMode, LocalPhysicsMode.None, suspendLoad, priority); @@ -77,15 +145,13 @@ namespace TEngine } _subScenes.Add(location, subScene); - - return default; } else { if (_currentMainScene is { IsDone: false }) { Log.Warning($"Could not load MainScene while loading. CurrentMainScene: {_currentMainSceneName}."); - return default; + return; } _currentMainSceneName = location; @@ -103,8 +169,6 @@ namespace TEngine } ModuleSystem.GetModule().ForceUnloadUnusedAssets(gcCollect); - - return _currentMainScene.SceneObject; } }