pull/229/head
hevinci 2023-10-26 17:11:56 +08:00
parent 64681db027
commit 460ea091bd
14 changed files with 115 additions and 73 deletions

View File

@ -37,6 +37,19 @@ namespace YooAsset
}
}
/// <summary>
/// 场景名称
/// </summary>
public string SceneName
{
get
{
if (IsValidWithWarning == false)
return string.Empty;
return Provider.SceneName;
}
}
/// <summary>
/// 场景对象
/// </summary>
@ -152,10 +165,8 @@ namespace YooAsset
}
// 卸载子场景
Scene sceneObject = SceneObject;
Provider.Impl.UnloadSubScene(Provider);
{
var operation = new UnloadSceneOperation(sceneObject);
var operation = new UnloadSceneOperation(Provider);
OperationSystem.StartOperation(packageName, operation);
return operation;
}

View File

@ -8,68 +8,81 @@ namespace YooAsset
/// </summary>
public sealed class UnloadSceneOperation : AsyncOperationBase
{
private enum EFlag
{
Normal,
Error,
}
private enum ESteps
{
None,
UnLoad,
CheckError,
PrepareDone,
UnLoadScene,
Checking,
Done,
}
private readonly EFlag _flag;
private ESteps _steps = ESteps.None;
private Scene _scene;
private readonly string _error;
private readonly ProviderBase _provider;
private AsyncOperation _asyncOp;
internal UnloadSceneOperation(string error)
{
_flag = EFlag.Error;
Error = error;
_error = error;
}
internal UnloadSceneOperation(Scene scene)
internal UnloadSceneOperation(ProviderBase provider)
{
_flag = EFlag.Normal;
_scene = scene;
_error = null;
_provider = provider;
}
internal override void InternalOnStart()
{
if (_flag == EFlag.Normal)
{
_steps = ESteps.UnLoad;
}
else if (_flag == EFlag.Error)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
else
{
throw new System.NotImplementedException(_flag.ToString());
}
_steps = ESteps.CheckError;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.UnLoad)
if (_steps == ESteps.CheckError)
{
if (_scene.IsValid() && _scene.isLoaded)
{
_asyncOp = SceneManager.UnloadSceneAsync(_scene);
_steps = ESteps.Checking;
}
else
if (string.IsNullOrEmpty(_error) == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Scene is invalid or is not loaded.";
Error = _error;
return;
}
_steps = ESteps.PrepareDone;
}
if(_steps == ESteps.PrepareDone)
{
if (_provider.IsDone == false)
return;
if (_provider.SceneObject.IsValid() == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Scene is invalid !";
return;
}
if (_provider.SceneObject.isLoaded == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Scene is not loaded !";
return;
}
_steps = ESteps.UnLoadScene;
}
if (_steps == ESteps.UnLoadScene)
{
_asyncOp = SceneManager.UnloadSceneAsync(_provider.SceneObject);
_provider.ResourceMgr.UnloadSubScene(_provider.SceneName);
_steps = ESteps.Checking;
}
if (_steps == ESteps.Checking)

View File

@ -8,7 +8,7 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledAllAssetsProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -8,7 +8,7 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledAssetProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public BundledAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -3,7 +3,7 @@ namespace YooAsset
{
internal class BundledRawFileProvider : ProviderBase
{
public BundledRawFileProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public BundledRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -9,14 +9,13 @@ namespace YooAsset
internal sealed class BundledSceneProvider : ProviderBase
{
public readonly LoadSceneMode SceneMode;
private readonly string _sceneName;
private readonly bool _suspendLoad;
private AsyncOperation _asyncOperation;
public BundledSceneProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(impl, providerGUID, providerPriority, assetInfo)
public BundledSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo)
{
SceneMode = sceneMode;
_sceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad;
}
public override void Update()
@ -73,7 +72,7 @@ namespace YooAsset
else
{
Status = EStatus.Failed;
LastError = $"Failed to load scene : {_sceneName}";
LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
}

View File

@ -8,7 +8,7 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledSubAssetsProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -6,7 +6,7 @@ namespace YooAsset
{
internal sealed class DatabaseAllAssetsProvider : ProviderBase
{
public DatabaseAllAssetsProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -6,7 +6,7 @@ namespace YooAsset
{
internal sealed class DatabaseAssetProvider : ProviderBase
{
public DatabaseAssetProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public DatabaseAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -3,7 +3,7 @@ namespace YooAsset
{
internal class DatabaseRawFileProvider : ProviderBase
{
public DatabaseRawFileProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -1,4 +1,7 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset
@ -9,9 +12,10 @@ namespace YooAsset
private readonly bool _suspendLoad;
private AsyncOperation _asyncOperation;
public DatabaseSceneProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(impl, providerGUID, providerPriority, assetInfo)
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo)
{
SceneMode = sceneMode;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad;
}
public override void Update()

View File

@ -6,7 +6,7 @@ namespace YooAsset
{
internal sealed class DatabaseSubAssetsProvider : ProviderBase
{
public DatabaseSubAssetsProvider(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(impl, providerGUID, providerPriority, assetInfo)
public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
{
}
public override void Update()

View File

@ -31,7 +31,7 @@ namespace YooAsset
/// <summary>
/// 所属资源系统
/// </summary>
public ResourceManager Impl { private set; get; }
public ResourceManager ResourceMgr { private set; get; }
/// <summary>
/// 资源信息
@ -53,6 +53,11 @@ namespace YooAsset
/// </summary>
public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; }
/// <summary>
/// 加载的场景名称
/// </summary>
public string SceneName { protected set; get; }
/// <summary>
/// 原生文件路径
/// </summary>
@ -103,21 +108,21 @@ namespace YooAsset
private readonly List<HandleBase> _handles = new List<HandleBase>();
public ProviderBase(ResourceManager impl, string providerGUID, uint providerPriority, AssetInfo assetInfo)
public ProviderBase(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo)
{
Impl = impl;
ResourceMgr = manager;
ProviderGUID = providerGUID;
ProviderPriority = providerPriority;
MainAssetInfo = assetInfo;
// 创建资源包加载器
if (impl != null)
if (manager != null)
{
OwnerBundle = impl.CreateOwnerAssetBundleLoader(assetInfo);
OwnerBundle = manager.CreateOwnerAssetBundleLoader(assetInfo);
OwnerBundle.Reference();
OwnerBundle.AddProvider(this);
var dependList = impl.CreateDependAssetBundleLoaders(assetInfo);
var dependList = manager.CreateDependAssetBundleLoaders(assetInfo);
DependBundles = new DependAssetBundles(dependList);
DependBundles.Reference();
}
@ -128,14 +133,6 @@ namespace YooAsset
/// </summary>
public abstract void Update();
/// <summary>
/// 排序接口实现方法
/// </summary>
public int CompareTo(ProviderBase other)
{
return other.ProviderPriority.CompareTo(this.ProviderPriority);
}
/// <summary>
/// 销毁资源提供者
/// </summary>
@ -281,6 +278,13 @@ namespace YooAsset
}
}
#region 排序接口实现
public int CompareTo(ProviderBase other)
{
return other.ProviderPriority.CompareTo(this.ProviderPriority);
}
#endregion
#region 异步编程相关
private TaskCompletionSource<object> _taskCompletionSource;
protected void InvokeCompletion()

View File

@ -218,7 +218,9 @@ namespace YooAsset
}
/// <summary>
/// 加载场景
/// 加载场景对象
/// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。
/// 注意:业务逻辑层应该避免同时加载一个子场景。
/// </summary>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, uint priority)
{
@ -387,15 +389,24 @@ namespace YooAsset
return provider.CreateHandle<RawFileHandle>();
}
internal void UnloadSubScene(ProviderBase provider)
internal void UnloadSubScene(string sceneName)
{
List<string> removeKeys = new List<string>();
foreach (var valuePair in _sceneHandles)
{
var sceneHandle = valuePair.Value;
if (sceneHandle.SceneName == sceneName)
{
string providerGUID = provider.ProviderGUID;
if (_sceneHandles.ContainsKey(providerGUID) == false)
throw new Exception("Should never get here !");
// 释放子场景句柄
_sceneHandles[providerGUID].ReleaseInternal();
_sceneHandles.Remove(providerGUID);
sceneHandle.ReleaseInternal();
removeKeys.Add(valuePair.Key);
}
}
foreach (string key in removeKeys)
{
_sceneHandles.Remove(key);
}
}
private void UnloadAllScene()
{