Compare commits

..

3 Commits

Author SHA1 Message Date
何冠峰
4c717b69db Update CHANGELOG.md 2026-05-09 17:39:32 +08:00
何冠峰
d64997a0df Update package.json 2026-05-09 17:38:36 +08:00
何冠峰
a265b85d37 refactor : 代码重构 2026-05-09 17:30:10 +08:00
10 changed files with 113 additions and 41 deletions

View File

@@ -2,6 +2,6 @@
All notable changes to this package will be documented in this file. All notable changes to this package will be documented in this file.
## [3.0.0] - 2026-05-01 ## [3.0.0-beta] - 2026-05-09
beta released. beta released.

View File

@@ -33,6 +33,11 @@ namespace YooAsset
/// </summary> /// </summary>
public bool IsDestroyed { private set; get; } = false; public bool IsDestroyed { private set; get; } = false;
/// <summary>
/// 是否已收到强制销毁请求
/// </summary>
public bool ForceDestroyRequested { private set; get; } = false;
/// <summary> /// <summary>
/// 引用计数 /// 引用计数
/// </summary> /// </summary>
@@ -57,6 +62,25 @@ namespace YooAsset
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (ForceDestroyRequested)
{
if (_steps == ESteps.CheckConcurrency)
{
_steps = ESteps.Done;
SetError("Bundle loader force destroyed during package destruction.");
return;
}
if (_steps == ESteps.LoadBundleFile)
{
// 注意:终止下载器
if (_loadPackageBundleOp != null)
_loadPackageBundleOp.ShouldAbortDownload = true;
}
// 注意:其它条件的情况下,继续往下走,等底层操作自然退出。
}
if (_steps == ESteps.CheckConcurrency) if (_steps == ESteps.CheckConcurrency)
{ {
if (IsWaitForCompletion) if (IsWaitForCompletion)
@@ -141,8 +165,14 @@ namespace YooAsset
/// <summary> /// <summary>
/// 销毁资源包加载器并释放资源包 /// 销毁资源包加载器并释放资源包
/// </summary> /// </summary>
/// <remarks>
/// 该方法是幂等的,重复调用不会重复释放资源。
/// </remarks>
public void DestroyLoader() public void DestroyLoader()
{ {
if (IsDestroyed)
return;
IsDestroyed = true; IsDestroyed = true;
// 注意:正在加载中的任务不可以销毁 // 注意:正在加载中的任务不可以销毁
@@ -163,10 +193,16 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 强制销毁资源包加载器(仅用于全局 Destroy 场景) /// 强制销毁资源包加载器
/// </summary> /// </summary>
/// <remarks>
/// 该方法是幂等的,仅用于全局 Destroy 场景,重复调用不会重复释放资源。
/// </remarks>
public void ForceDestroyLoader() public void ForceDestroyLoader()
{ {
if (IsDestroyed)
return;
IsDestroyed = true; IsDestroyed = true;
if (_steps == ESteps.LoadBundleFile) if (_steps == ESteps.LoadBundleFile)
@@ -186,6 +222,14 @@ namespace YooAsset
} }
} }
/// <summary>
/// 请求强制销毁
/// </summary>
public void RequestForceDestroy()
{
ForceDestroyRequested = true;
}
/// <summary> /// <summary>
/// 是否可以销毁 /// 是否可以销毁
/// </summary> /// </summary>
@@ -271,26 +315,5 @@ namespace YooAsset
} }
} }
/// <summary>
/// 尝试终止加载器
/// </summary>
public void TryAbortLoader()
{
if (IsDone == false)
{
if (_steps == ESteps.CheckConcurrency)
{
_steps = ESteps.Done;
SetError("Bundle loader aborted.");
}
if (_steps == ESteps.LoadBundleFile)
{
// 注意:终止下载器
if (_loadPackageBundleOp != null)
_loadPackageBundleOp.ShouldAbortDownload = true;
}
}
}
} }
} }

View File

@@ -13,7 +13,7 @@ namespace YooAsset
None, None,
CheckOptions, CheckOptions,
ReleaseAll, ReleaseAll,
TryAbortLoader, RequestForceDestroy,
CheckLoading, CheckLoading,
DestroyAll, DestroyAll,
Done, Done,
@@ -57,14 +57,15 @@ namespace YooAsset
if (_options.ShouldReleaseHandles) if (_options.ShouldReleaseHandles)
_resourceManager.ReleaseAllHandles(); _resourceManager.ReleaseAllHandles();
_steps = ESteps.TryAbortLoader; _steps = ESteps.RequestForceDestroy;
} }
if (_steps == ESteps.TryAbortLoader) if (_steps == ESteps.RequestForceDestroy)
{ {
// 尝试终止所有加载任务 // 向所有 Provider 和 BundleLoader 下发强制销毁请求
// 注意正在加载AssetBundle的任务无法终止 _resourceManager.RequestForceDestroyAllProviders();
_resourceManager.TryAbortAllBundleLoaders(); _resourceManager.RequestForceDestroyAllBundleLoaders();
_steps = ESteps.CheckLoading; _steps = ESteps.CheckLoading;
} }

View File

@@ -70,6 +70,11 @@ namespace YooAsset
/// </summary> /// </summary>
public bool IsDestroyed { private set; get; } = false; public bool IsDestroyed { private set; get; } = false;
/// <summary>
/// 是否已收到强制销毁请求
/// </summary>
public bool ForceDestroyRequested { private set; get; } = false;
/// <summary> /// <summary>
/// 加载任务是否进行中 /// 加载任务是否进行中
/// </summary> /// </summary>
@@ -127,7 +132,18 @@ namespace YooAsset
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
// 注意:未在加载中的任务可以挂起! if (ForceDestroyRequested)
{
if (IsLoading == false)
{
SetFail("Provider force destroyed during package destruction.");
return;
}
// 注意:已进入加载阶段则继续等待自然完成
}
// 注意:未在加载中的任务可以挂起
if (IsLoading == false) if (IsLoading == false)
{ {
if (RefCount <= 0) if (RefCount <= 0)
@@ -206,8 +222,14 @@ namespace YooAsset
/// <summary> /// <summary>
/// 销毁资源提供者 /// 销毁资源提供者
/// </summary> /// </summary>
/// <remarks>
/// 该方法是幂等的,重复调用不会重复释放资源。
/// </remarks>
public void DestroyProvider() public void DestroyProvider()
{ {
if (IsDestroyed)
return;
IsDestroyed = true; IsDestroyed = true;
// 检测是否为正常销毁 // 检测是否为正常销毁
@@ -224,6 +246,14 @@ namespace YooAsset
} }
} }
/// <summary>
/// 请求强制销毁
/// </summary>
public void RequestForceDestroy()
{
ForceDestroyRequested = true;
}
/// <summary> /// <summary>
/// 是否可以销毁 /// 是否可以销毁
/// </summary> /// </summary>

View File

@@ -449,13 +449,24 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 尝试终止所有 BundleLoader /// 向所有 Provider 下发强制销毁请求
/// </summary> /// </summary>
internal void TryAbortAllBundleLoaders() internal void RequestForceDestroyAllProviders()
{
foreach (var provider in _providerDict.Values)
{
provider.RequestForceDestroy();
}
}
/// <summary>
/// 向所有 BundleLoader 下发强制销毁请求
/// </summary>
internal void RequestForceDestroyAllBundleLoaders()
{ {
foreach (var loader in _bundleLoaderDict.Values) foreach (var loader in _bundleLoaderDict.Values)
{ {
loader.TryAbortLoader(); loader.RequestForceDestroy();
} }
} }

View File

@@ -16,14 +16,17 @@ namespace YooAsset
} }
private readonly ResourcePackage _resourcePackage; private readonly ResourcePackage _resourcePackage;
private readonly ResourceManager _resourceManager;
private readonly UnloadAllAssetsOptions _options; private readonly UnloadAllAssetsOptions _options;
private UnloadAllAssetsOperation _unloadAllAssetsOp; private UnloadAllAssetsOperation _unloadAllAssetsOp;
private string _packageVersion = string.Empty;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal DestroyPackageOperation(ResourcePackage resourcePackage, UnloadAllAssetsOptions options) internal DestroyPackageOperation(ResourcePackage resourcePackage, ResourceManager resourceManager, UnloadAllAssetsOptions options)
{ {
_resourcePackage = resourcePackage; _resourcePackage = resourcePackage;
_resourceManager = resourceManager;
_options = options; _options = options;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -53,9 +56,14 @@ namespace YooAsset
case EOperationStatus.Succeeded: case EOperationStatus.Succeeded:
if (_resourcePackage.PackageValid) if (_resourcePackage.PackageValid)
{
_packageVersion = _resourcePackage.GetPackageVersion();
_steps = ESteps.UnloadAllAssets; _steps = ESteps.UnloadAllAssets;
}
else else
{
_steps = ESteps.DestroyPackage; _steps = ESteps.DestroyPackage;
}
break; break;
default: default:
@@ -67,7 +75,7 @@ namespace YooAsset
{ {
if (_unloadAllAssetsOp == null) if (_unloadAllAssetsOp == null)
{ {
_unloadAllAssetsOp = _resourcePackage.UnloadAllAssetsAsync(_options); _unloadAllAssetsOp = new UnloadAllAssetsOperation(_resourceManager, _options);
_unloadAllAssetsOp.StartOperation(); _unloadAllAssetsOp.StartOperation();
AddChildOperation(_unloadAllAssetsOp); AddChildOperation(_unloadAllAssetsOp);
} }
@@ -89,7 +97,6 @@ namespace YooAsset
if (_steps == ESteps.DestroyPackage) if (_steps == ESteps.DestroyPackage)
{ {
// 销毁包裹
_resourcePackage.InternalDestroy(); _resourcePackage.InternalDestroy();
// 最后清理该包裹的异步任务 // 最后清理该包裹的异步任务
@@ -103,7 +110,7 @@ namespace YooAsset
/// <inheritdoc /> /// <inheritdoc />
protected override string InternalGetDescription() protected override string InternalGetDescription()
{ {
return $"PackageVersion: {_resourcePackage.GetPackageVersion()}"; return $"PackageVersion: {_packageVersion}";
} }
} }
} }

View File

@@ -120,7 +120,7 @@ namespace YooAsset
public DestroyPackageOperation DestroyPackageAsync() public DestroyPackageOperation DestroyPackageAsync()
{ {
var options = new UnloadAllAssetsOptions(true, true); var options = new UnloadAllAssetsOptions(true, true);
var operation = new DestroyPackageOperation(this, options); var operation = new DestroyPackageOperation(this, _resourceManager, options);
AsyncOperationSystem.StartOperation(AsyncOperationSystem.GlobalSchedulerName, operation); AsyncOperationSystem.StartOperation(AsyncOperationSystem.GlobalSchedulerName, operation);
return operation; return operation;
} }

View File

@@ -30,7 +30,7 @@ namespace YooAsset.Editor
{ {
GUILayout.Space(10); GUILayout.Space(10);
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Select Builtin Resource Directory", GUILayout.MaxWidth(150))) if (GUILayout.Button("Select Builtin Resource Directory", GUILayout.MaxWidth(250)))
{ {
string resultPath = EditorUtility.OpenFolderPanel("Find", "Assets/", "StreamingAssets"); string resultPath = EditorUtility.OpenFolderPanel("Find", "Assets/", "StreamingAssets");
if (!string.IsNullOrEmpty(resultPath)) if (!string.IsNullOrEmpty(resultPath))

View File

@@ -47,7 +47,7 @@ public class TestLoadScene
// 同步加载附加场景并等待 // 同步加载附加场景并等待
yield return new WaitForSeconds(0.2f); yield return new WaitForSeconds(0.2f);
SceneHandle cachedHandle; YooAsset.SceneHandle cachedHandle;
{ {
cachedHandle = package.LoadSceneSync("scene_c", LoadSceneMode.Additive); cachedHandle = package.LoadSceneSync("scene_c", LoadSceneMode.Additive);
yield return cachedHandle; yield return cachedHandle;

View File

@@ -1,7 +1,7 @@
{ {
"name": "com.tuyoogame.yooasset", "name": "com.tuyoogame.yooasset",
"displayName": "YooAsset", "displayName": "YooAsset",
"version": "3.0.0", "version": "3.0.0-beta",
"unity": "2019.4", "unity": "2019.4",
"description": "unity3d resources management system.", "description": "unity3d resources management system.",
"author": { "author": {