mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
移除资源模块加载场景,加载场景统一走场景管理模块。增加场景加载进度回调。
移除资源模块加载场景,加载场景统一走场景管理模块。增加场景加载进度回调。
This commit is contained in:
@@ -248,27 +248,6 @@ namespace TEngine
|
||||
/// <returns>资源对象集合。</returns>
|
||||
UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag) where T : UnityEngine.Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起。</param>
|
||||
/// <param name="priority">优先级。</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100);
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景.
|
||||
/// </summary>
|
||||
/// <param name="assetInfo">场景的资源信息。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起。</param>
|
||||
/// <param name="priority">优先级。</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,
|
||||
int priority = 100);
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源。
|
||||
/// </summary>
|
||||
@@ -325,17 +304,5 @@ namespace TEngine
|
||||
/// <typeparam name="T">资源实例类型。</typeparam>
|
||||
/// <returns>原生文件资源实例。</returns>
|
||||
UniTask<T[]> LoadAllSubAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="activateOnLoad">加载完毕时是否主动激活。</param>
|
||||
/// <param name="priority">优先级。</param>
|
||||
/// <returns>场景资源实例。</returns>
|
||||
UniTask<Scene> LoadSceneAsyncByUniTask(string location, CancellationToken cancellationToken = default, LoadSceneMode sceneMode = LoadSceneMode.Single,
|
||||
bool activateOnLoad = true, int priority = 100);
|
||||
}
|
||||
}
|
@@ -726,35 +726,6 @@ namespace TEngine
|
||||
return assetObjects;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起。</param>
|
||||
/// <param name="priority">加载优先级。</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
public SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,
|
||||
int priority = 100)
|
||||
{
|
||||
return YooAssets.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="assetInfo">场景的资源信息。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起。</param>
|
||||
/// <param name="priority">加载优先级。</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
public SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,
|
||||
int priority = 100)
|
||||
{
|
||||
return YooAssets.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源实例。
|
||||
/// </summary>
|
||||
@@ -910,26 +881,6 @@ namespace TEngine
|
||||
|
||||
return cancelOrFailed ? null : handle.GetSubAssetObjects<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="activateOnLoad">加载完毕时是否主动激活。</param>
|
||||
/// <param name="priority">加载优先级。</param>
|
||||
/// <returns>场景资源实例。</returns>
|
||||
public async UniTask<Scene> LoadSceneAsyncByUniTask(string location, CancellationToken cancellationToken = default,
|
||||
LoadSceneMode sceneMode = LoadSceneMode.Single,
|
||||
bool activateOnLoad = true, int priority = 100)
|
||||
{
|
||||
SceneOperationHandle handle = YooAssets.LoadSceneAsync(location, sceneMode, activateOnLoad, priority);
|
||||
|
||||
bool cancelOrFailed = await handle.ToUniTask().AttachExternalCancellation(cancellationToken).SuppressCancellationThrow();
|
||||
|
||||
return cancelOrFailed ? default : handle.SceneObject;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -584,34 +584,6 @@ namespace TEngine
|
||||
return await m_ResourceManager.LoadAssetsByTagAsync<T>(assetTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址</param>
|
||||
/// <param name="sceneMode">场景加载模式</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起</param>
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
public SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,
|
||||
int priority = 100)
|
||||
{
|
||||
return m_ResourceManager.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景
|
||||
/// </summary>
|
||||
/// <param name="assetInfo">场景的资源信息</param>
|
||||
/// <param name="sceneMode">场景加载模式</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起</param>
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
public SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,
|
||||
int priority = 100)
|
||||
{
|
||||
return m_ResourceManager.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源。
|
||||
/// </summary>
|
||||
@@ -686,20 +658,5 @@ namespace TEngine
|
||||
{
|
||||
return await m_ResourceManager.LoadAllSubAssetAsync<T>(location, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="sceneMode">场景加载模式。</param>
|
||||
/// <param name="activateOnLoad">加载完毕时是否主动激活。</param>
|
||||
/// <param name="priority">优先级。</param>
|
||||
/// <returns>场景资源实例。</returns>
|
||||
public async UniTask<Scene> LoadSceneAsyncByUniTask(string location, CancellationToken cancellationToken = default,
|
||||
LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
|
||||
{
|
||||
return await m_ResourceManager.LoadSceneAsyncByUniTask(location, cancellationToken, sceneMode, activateOnLoad, priority);
|
||||
}
|
||||
}
|
||||
}
|
@@ -20,8 +20,14 @@ namespace TEngine
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <param name="callBack">加载回调。</param>
|
||||
/// <param name="gcCollect">加载主场景是否回收垃圾。</param>
|
||||
public void LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null, bool gcCollect = true);
|
||||
/// <param name="progressCallBack">加载进度回调。</param>
|
||||
public SceneOperationHandle LoadScene(string location,
|
||||
LoadSceneMode sceneMode = LoadSceneMode.Single,
|
||||
bool suspendLoad = false,
|
||||
int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null,
|
||||
bool gcCollect = true,
|
||||
Action<float> progressCallBack = null);
|
||||
|
||||
/// <summary>
|
||||
/// 激活场景(当同时存在多个场景时用于切换激活场景)。
|
||||
|
@@ -42,10 +42,11 @@ namespace TEngine
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <param name="callBack">加载回调。</param>
|
||||
/// <param name="gcCollect">加载主场景是否回收垃圾。</param>
|
||||
public void LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null, bool gcCollect = true)
|
||||
/// <param name="progressCallBack">加载进度回调。</param>
|
||||
public SceneOperationHandle LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null, bool gcCollect = true, Action<float> progressCallBack = null)
|
||||
{
|
||||
_sceneModule.LoadScene(location, sceneMode, suspendLoad, priority, callBack, gcCollect);
|
||||
return _sceneModule.LoadScene(location, sceneMode, suspendLoad, priority, callBack, gcCollect, progressCallBack);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,10 +57,11 @@ namespace TEngine
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <param name="callBack">加载回调。</param>
|
||||
/// <param name="gcCollect">加载主场景是否回收垃圾。</param>
|
||||
public void LoadSubScene(string location, bool suspendLoad = false, int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null, bool gcCollect = true)
|
||||
/// <param name="progressCallBack">加载进度回调。</param>
|
||||
public SceneOperationHandle LoadSubScene(string location, bool suspendLoad = false, int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null, bool gcCollect = true, Action<float> progressCallBack = null)
|
||||
{
|
||||
_sceneModule.LoadScene(location, LoadSceneMode.Additive, suspendLoad, priority, callBack, gcCollect);
|
||||
return _sceneModule.LoadScene(location, LoadSceneMode.Additive, suspendLoad, priority, callBack, gcCollect, progressCallBack);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.SceneManagement;
|
||||
using YooAsset;
|
||||
|
||||
@@ -8,7 +9,7 @@ namespace TEngine
|
||||
/// <summary>
|
||||
/// 场景管理器。
|
||||
/// </summary>
|
||||
internal class SceneModuleImp : ModuleImp,ISceneModule
|
||||
internal class SceneModuleImp : ModuleImp, ISceneModule
|
||||
{
|
||||
private string _currentMainSceneName = string.Empty;
|
||||
|
||||
@@ -40,40 +41,83 @@ namespace TEngine
|
||||
/// <summary>
|
||||
/// 加载场景。
|
||||
/// </summary>
|
||||
/// <param name="location">场景的定位地址</param>
|
||||
/// <param name="location">场景的定位地址</param>R
|
||||
/// <param name="sceneMode">场景加载模式</param>
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起</param>
|
||||
/// <param name="priority">优先级</param>
|
||||
/// <param name="callBack">加载回调。</param>
|
||||
/// <param name="gcCollect">加载主场景是否回收垃圾。</param>
|
||||
public void LoadScene(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100, Action<SceneOperationHandle> callBack = null,bool gcCollect = true)
|
||||
/// <param name="progressCallBack">加载进度回调。</param>
|
||||
public SceneOperationHandle LoadScene(string location,
|
||||
LoadSceneMode sceneMode = LoadSceneMode.Single,
|
||||
bool suspendLoad = false,
|
||||
int priority = 100,
|
||||
Action<SceneOperationHandle> callBack = null,
|
||||
bool gcCollect = true,
|
||||
Action<float> progressCallBack = null)
|
||||
{
|
||||
if (sceneMode == LoadSceneMode.Additive)
|
||||
{
|
||||
if (_subScenes.ContainsKey(location))
|
||||
if (_subScenes.TryGetValue(location, out SceneOperationHandle subScene))
|
||||
{
|
||||
Log.Warning($"Could not load subScene while already loaded. Scene: {location}");
|
||||
return;
|
||||
return subScene;
|
||||
}
|
||||
var subScene = GameModule.Resource.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
subScene = YooAssets.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
|
||||
if (callBack != null)
|
||||
{
|
||||
subScene.Completed += callBack;
|
||||
}
|
||||
|
||||
if (progressCallBack != null)
|
||||
{
|
||||
InvokeProgress(subScene, progressCallBack).Forget();
|
||||
}
|
||||
_subScenes.Add(location, subScene);
|
||||
|
||||
return subScene;
|
||||
}
|
||||
else if(sceneMode == LoadSceneMode.Single)
|
||||
else
|
||||
{
|
||||
if (_currentMainScene is { IsDone: false })
|
||||
{
|
||||
Log.Warning($"Could not load MainScene while loading. CurrentMainScene: {_currentMainSceneName}.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_currentMainSceneName = location;
|
||||
|
||||
_currentMainScene = GameModule.Resource.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
_currentMainScene = YooAssets.LoadSceneAsync(location, sceneMode, suspendLoad, priority);
|
||||
|
||||
if (callBack != null)
|
||||
{
|
||||
_currentMainScene.Completed += callBack;
|
||||
}
|
||||
|
||||
if (progressCallBack != null)
|
||||
{
|
||||
InvokeProgress(_currentMainScene, progressCallBack).Forget();
|
||||
}
|
||||
|
||||
GameModule.Resource.ForceUnloadUnusedAssets(gcCollect);
|
||||
|
||||
return _currentMainScene;
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTaskVoid InvokeProgress(SceneOperationHandle sceneOperationHandle,Action<float> progress)
|
||||
{
|
||||
if (sceneOperationHandle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while (!sceneOperationHandle.IsDone)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
|
||||
progress?.Invoke(sceneOperationHandle.Progress);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user