优化场景挂起流程,支持中途取消挂起操作。
pull/274/head
hevinci 2024-04-03 17:11:11 +08:00
parent f06bd83dc3
commit b5ffd5005a
3 changed files with 40 additions and 36 deletions

View File

@ -100,29 +100,21 @@ namespace YooAsset
if (IsValidWithWarning == false) if (IsValidWithWarning == false)
return false; return false;
// 注意:当场景挂起操作执行之后无法中途取消!
if (SceneObject.IsValid())
{
if (Provider is DatabaseSceneProvider) if (Provider is DatabaseSceneProvider)
{ {
var temp = Provider as DatabaseSceneProvider; var provider = Provider as DatabaseSceneProvider;
return temp.UnSuspendLoad(); provider.UnSuspendLoad();
} }
else if (Provider is BundledSceneProvider) else if (Provider is BundledSceneProvider)
{ {
var temp = Provider as BundledSceneProvider; var provider = Provider as BundledSceneProvider;
return temp.UnSuspendLoad(); provider.UnSuspendLoad();
} }
else else
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
} return true;
else
{
YooLogger.Warning($"Scene is invalid : {SceneObject.name}");
return false;
}
} }
/// <summary> /// <summary>

View File

@ -9,14 +9,14 @@ namespace YooAsset
internal sealed class BundledSceneProvider : ProviderBase internal sealed class BundledSceneProvider : ProviderBase
{ {
public readonly LoadSceneMode SceneMode; public readonly LoadSceneMode SceneMode;
private readonly bool _suspendLoad;
private AsyncOperation _asyncOperation; private AsyncOperation _asyncOperation;
private bool _suspendLoadMode;
public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{ {
SceneMode = sceneMode; SceneMode = sceneMode;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad; _suspendLoadMode = suspendLoad;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -80,7 +80,7 @@ namespace YooAsset
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode); _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
if (_asyncOperation != null) if (_asyncOperation != null)
{ {
_asyncOperation.allowSceneActivation = !_suspendLoad; _asyncOperation.allowSceneActivation = !_suspendLoadMode;
_asyncOperation.priority = 100; _asyncOperation.priority = 100;
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
_steps = ESteps.Checking; _steps = ESteps.Checking;
@ -106,6 +106,13 @@ namespace YooAsset
} }
else else
{ {
// 注意:在业务层中途可以取消挂起
if (_asyncOperation.allowSceneActivation == false)
{
if (_suspendLoadMode == false)
_asyncOperation.allowSceneActivation = true;
}
Progress = _asyncOperation.progress; Progress = _asyncOperation.progress;
if (_asyncOperation.isDone == false) if (_asyncOperation.isDone == false)
return; return;
@ -128,13 +135,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 解除场景加载挂起操作 /// 解除场景加载挂起操作
/// </summary> /// </summary>
public bool UnSuspendLoad() public void UnSuspendLoad()
{ {
if (_asyncOperation == null) if (IsDone == false)
return false; {
_suspendLoadMode = false;
_asyncOperation.allowSceneActivation = true; }
return true;
} }
} }
} }

View File

@ -9,14 +9,14 @@ namespace YooAsset
internal sealed class DatabaseSceneProvider : ProviderBase internal sealed class DatabaseSceneProvider : ProviderBase
{ {
public readonly LoadSceneMode SceneMode; public readonly LoadSceneMode SceneMode;
private readonly bool _suspendLoad; private bool _suspendLoadMode;
private AsyncOperation _asyncOperation; private AsyncOperation _asyncOperation;
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{ {
SceneMode = sceneMode; SceneMode = sceneMode;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad; _suspendLoadMode = suspendLoad;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -69,7 +69,7 @@ namespace YooAsset
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters); _asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
if (_asyncOperation != null) if (_asyncOperation != null)
{ {
_asyncOperation.allowSceneActivation = !_suspendLoad; _asyncOperation.allowSceneActivation = !_suspendLoadMode;
_asyncOperation.priority = 100; _asyncOperation.priority = 100;
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
_steps = ESteps.Checking; _steps = ESteps.Checking;
@ -95,6 +95,13 @@ namespace YooAsset
} }
else else
{ {
// 注意:在业务层中途可以取消挂起
if (_asyncOperation.allowSceneActivation == false)
{
if (_suspendLoadMode == false)
_asyncOperation.allowSceneActivation = true;
}
Progress = _asyncOperation.progress; Progress = _asyncOperation.progress;
if (_asyncOperation.isDone == false) if (_asyncOperation.isDone == false)
return; return;
@ -118,13 +125,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 解除场景加载挂起操作 /// 解除场景加载挂起操作
/// </summary> /// </summary>
public bool UnSuspendLoad() public void UnSuspendLoad()
{ {
if (_asyncOperation == null) if (IsDone == false)
return false; {
_suspendLoadMode = false;
_asyncOperation.allowSceneActivation = true; }
return true;
} }
} }
} }