refactor load scene code

pull/342/head
hevinci 2024-08-03 16:53:25 +08:00
parent 4f58c54eff
commit b89f00130e
5 changed files with 49 additions and 28 deletions

View File

@ -8,14 +8,24 @@ namespace YooAsset
{ {
internal sealed class BundledSceneProvider : ProviderOperation internal sealed class BundledSceneProvider : ProviderOperation
{ {
public LoadSceneMode SceneMode => LoadSceneParams.loadSceneMode;
public readonly LoadSceneParameters LoadSceneParams; public readonly LoadSceneParameters LoadSceneParams;
private AsyncOperation _asyncOperation; private AsyncOperation _asyncOperation;
private bool _suspendLoadMode; private bool _suspendLoadMode;
public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, LocalPhysicsMode physicsMode = default) : base(manager, providerGUID, assetInfo) /// <summary>
/// 场景加载模式
/// </summary>
public LoadSceneMode SceneMode
{ {
LoadSceneParams = new LoadSceneParameters(sceneMode, physicsMode); get
{
return LoadSceneParams.loadSceneMode;
}
}
public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneParameters loadSceneParams, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{
LoadSceneParams = loadSceneParams;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoadMode = suspendLoad; _suspendLoadMode = suspendLoad;
} }

View File

@ -8,14 +8,24 @@ namespace YooAsset
{ {
internal sealed class DatabaseSceneProvider : ProviderOperation internal sealed class DatabaseSceneProvider : ProviderOperation
{ {
public LoadSceneMode SceneMode => LoadSceneParams.loadSceneMode; public readonly LoadSceneParameters LoadSceneParams;
public readonly LoadSceneParameters LoadSceneParams;
private bool _suspendLoadMode;
private AsyncOperation _asyncOperation; private AsyncOperation _asyncOperation;
private bool _suspendLoadMode;
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, LocalPhysicsMode physicsMode = default) : base(manager, providerGUID, assetInfo) /// <summary>
/// 场景加载模式
/// </summary>
public LoadSceneMode SceneMode
{ {
LoadSceneParams = new LoadSceneParameters(sceneMode, physicsMode); get
{
return LoadSceneParams.loadSceneMode;
}
}
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneParameters loadSceneParams, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{
LoadSceneParams = loadSceneParams;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoadMode = suspendLoad; _suspendLoadMode = suspendLoad;
} }

View File

@ -86,7 +86,7 @@ namespace YooAsset
/// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。 /// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。
/// 注意:业务逻辑层应该避免同时加载一个子场景。 /// 注意:业务逻辑层应该避免同时加载一个子场景。
/// </summary> /// </summary>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, uint priority, LocalPhysicsMode physicsMode = default) public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneParameters loadSceneParams, bool suspendLoad, uint priority)
{ {
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{ {
@ -97,7 +97,7 @@ namespace YooAsset
} }
// 如果加载的是主场景,则卸载所有缓存的场景 // 如果加载的是主场景,则卸载所有缓存的场景
if (sceneMode == LoadSceneMode.Single) if (loadSceneParams.loadSceneMode == LoadSceneMode.Single)
{ {
UnloadAllScene(); UnloadAllScene();
} }
@ -107,9 +107,9 @@ namespace YooAsset
ProviderOperation provider; ProviderOperation provider;
{ {
if (_simulationOnEditor) if (_simulationOnEditor)
provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad, physicsMode); provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, loadSceneParams, suspendLoad);
else else
provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad, physicsMode); provider = new BundledSceneProvider(this, providerGUID, assetInfo, loadSceneParams, suspendLoad);
provider.InitSpawnDebugInfo(); provider.InitSpawnDebugInfo();
_providerDic.Add(providerGUID, provider); _providerDic.Add(providerGUID, provider);
OperationSystem.StartOperation(PackageName, provider); OperationSystem.StartOperation(PackageName, provider);

View File

@ -486,11 +486,11 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="location">场景的定位地址</param> /// <param name="location">场景的定位地址</param>
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
public SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = default) public SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadSceneInternal(assetInfo, true, sceneMode, false, 0, physicsMode); return LoadSceneInternal(assetInfo, true, sceneMode, physicsMode, false, 0);
} }
/// <summary> /// <summary>
@ -498,10 +498,10 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="assetInfo">场景的资源信息</param> /// <param name="assetInfo">场景的资源信息</param>
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
public SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = default) public SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
return LoadSceneInternal(assetInfo, true, sceneMode, false, 0, physicsMode); return LoadSceneInternal(assetInfo, true, sceneMode, physicsMode, false, 0);
} }
/// <summary> /// <summary>
@ -511,11 +511,11 @@ namespace YooAsset
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param> /// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">加载的优先级</param> /// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0, LocalPhysicsMode physicsMode = default) public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 0)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadSceneInternal(assetInfo, false, sceneMode, suspendLoad, priority, physicsMode); return LoadSceneInternal(assetInfo, false, sceneMode, physicsMode, suspendLoad, priority);
} }
/// <summary> /// <summary>
@ -525,16 +525,17 @@ namespace YooAsset
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param> /// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">加载的优先级</param> /// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0, LocalPhysicsMode physicsMode = default) public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 0)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
return LoadSceneInternal(assetInfo, false, sceneMode, suspendLoad, priority, physicsMode); return LoadSceneInternal(assetInfo, false, sceneMode, physicsMode, suspendLoad, priority);
} }
private SceneHandle LoadSceneInternal(AssetInfo assetInfo, bool waitForAsyncComplete, LoadSceneMode sceneMode, bool suspendLoad, uint priority, LocalPhysicsMode physicsMode = default) private SceneHandle LoadSceneInternal(AssetInfo assetInfo, bool waitForAsyncComplete, LoadSceneMode sceneMode, LocalPhysicsMode physicsMode, bool suspendLoad, uint priority)
{ {
DebugCheckAssetLoadType(assetInfo.AssetType); DebugCheckAssetLoadType(assetInfo.AssetType);
var handle = _resourceManager.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority, physicsMode); var loadSceneParams = new LoadSceneParameters(sceneMode, physicsMode);
var handle = _resourceManager.LoadSceneAsync(assetInfo, loadSceneParams, suspendLoad, priority);
if (waitForAsyncComplete) if (waitForAsyncComplete)
handle.WaitForAsyncComplete(); handle.WaitForAsyncComplete();
return handle; return handle;

View File

@ -161,7 +161,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="location">场景的定位地址</param> /// <param name="location">场景的定位地址</param>
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
public static SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = default) public static SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{ {
DebugCheckDefaultPackageValid(); DebugCheckDefaultPackageValid();
return _defaultPackage.LoadSceneSync(location, sceneMode, physicsMode); return _defaultPackage.LoadSceneSync(location, sceneMode, physicsMode);
@ -172,7 +172,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="assetInfo">场景的资源信息</param> /// <param name="assetInfo">场景的资源信息</param>
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
public static SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = default) public static SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{ {
DebugCheckDefaultPackageValid(); DebugCheckDefaultPackageValid();
return _defaultPackage.LoadSceneSync(assetInfo, sceneMode, physicsMode); return _defaultPackage.LoadSceneSync(assetInfo, sceneMode, physicsMode);
@ -185,10 +185,10 @@ namespace YooAsset
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param> /// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">优先级</param> /// <param name="priority">优先级</param>
public static SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, LocalPhysicsMode physicsMode = default) public static SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 100)
{ {
DebugCheckDefaultPackageValid(); DebugCheckDefaultPackageValid();
return _defaultPackage.LoadSceneAsync(location, sceneMode, suspendLoad, priority, physicsMode); return _defaultPackage.LoadSceneAsync(location, sceneMode, physicsMode, suspendLoad, priority);
} }
/// <summary> /// <summary>
@ -198,10 +198,10 @@ namespace YooAsset
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param> /// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">优先级</param> /// <param name="priority">优先级</param>
public static SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100, LocalPhysicsMode physicsMode = default) public static SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 100)
{ {
DebugCheckDefaultPackageValid(); DebugCheckDefaultPackageValid();
return _defaultPackage.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority, physicsMode); return _defaultPackage.LoadSceneAsync(assetInfo, sceneMode, physicsMode, suspendLoad, priority);
} }
#endregion #endregion