Files
YooAsset/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs

104 lines
2.5 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset
{
2022-03-22 20:47:22 +08:00
internal sealed class DatabaseSceneProvider : ProviderBase
2022-03-01 10:44:12 +08:00
{
2022-03-22 20:15:59 +08:00
public readonly LoadSceneMode SceneMode;
private readonly bool _suspendLoad;
2022-03-22 20:15:59 +08:00
private readonly int _priority;
private AsyncOperation _asyncOperation;
2022-03-01 10:44:12 +08:00
public DatabaseSceneProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, int priority) : base(impl, providerGUID, assetInfo)
2022-03-01 10:44:12 +08:00
{
2022-03-22 20:15:59 +08:00
SceneMode = sceneMode;
_suspendLoad = suspendLoad;
2022-03-22 20:15:59 +08:00
_priority = priority;
2022-03-01 10:44:12 +08:00
}
public override void Update()
{
#if UNITY_EDITOR
if (IsDone)
return;
2022-03-09 23:57:04 +08:00
if (Status == EStatus.None)
2022-03-01 10:44:12 +08:00
{
Status = EStatus.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
OwnerBundle.WaitForAsyncComplete();
}
if (OwnerBundle.IsDone() == false)
return;
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
}
2022-03-09 23:57:04 +08:00
Status = EStatus.Loading;
2022-03-01 10:44:12 +08:00
}
// 2. 加载资源对象
2022-03-09 23:57:04 +08:00
if (Status == EStatus.Loading)
2022-03-01 10:44:12 +08:00
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
2022-03-22 20:15:59 +08:00
loadSceneParameters.loadSceneMode = SceneMode;
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
if (_asyncOperation != null)
2022-03-01 10:44:12 +08:00
{
_asyncOperation.allowSceneActivation = !_suspendLoad;
_asyncOperation.priority = _priority;
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
2022-03-09 23:57:04 +08:00
Status = EStatus.Checking;
2022-03-01 10:44:12 +08:00
}
else
{
Status = EStatus.Failed;
LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
2022-03-01 10:44:12 +08:00
InvokeCompletion();
}
}
// 3. 检测加载结果
2022-03-09 23:57:04 +08:00
if (Status == EStatus.Checking)
2022-03-01 10:44:12 +08:00
{
Progress = _asyncOperation.progress;
if (_asyncOperation.isDone)
{
Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed;
if (Status == EStatus.Failed)
{
LastError = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
}
2022-03-01 10:44:12 +08:00
InvokeCompletion();
}
}
#endif
}
/// <summary>
/// 解除场景加载挂起操作
/// </summary>
public bool UnSuspendLoad()
{
if (_asyncOperation == null)
return false;
_asyncOperation.allowSceneActivation = true;
return true;
}
2022-03-01 10:44:12 +08:00
}
}