feat : 弱引用资源句柄

This commit is contained in:
何冠峰
2025-09-09 18:31:02 +08:00
parent a71c46d1c4
commit 19f1d67a61
6 changed files with 126 additions and 19 deletions

View File

@@ -47,6 +47,15 @@ namespace YooAsset
/// WebGL平台强制同步加载资源对象
/// </summary>
public bool WebGLForceSyncLoadAsset = false;
#if YOOASSET_EXPERIMENTAL
/// <summary>
/// 启用弱引用资源句柄
/// </summary>
public bool UseWeakReferenceHandle = false;
#else
internal bool UseWeakReferenceHandle = false;
#endif
}
/// <summary>

View File

@@ -17,7 +17,7 @@ namespace YooAsset
AddChildOperation(_loadAllAssetsOp);
#if UNITY_WEBGL
if (_resManager.WebGLForceSyncLoadAsset())
if (_resManager.WebGLForceSyncLoadAsset)
_loadAllAssetsOp.WaitForAsyncComplete();
#endif
}

View File

@@ -17,7 +17,7 @@ namespace YooAsset
AddChildOperation(_loadAssetOp);
#if UNITY_WEBGL
if (_resManager.WebGLForceSyncLoadAsset())
if (_resManager.WebGLForceSyncLoadAsset)
_loadAssetOp.WaitForAsyncComplete();
#endif
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System;
namespace YooAsset
{
@@ -72,6 +73,7 @@ namespace YooAsset
private readonly LoadBundleFileOperation _mainBundleLoader;
private readonly List<LoadBundleFileOperation> _bundleLoaders = new List<LoadBundleFileOperation>(10);
private readonly HashSet<HandleBase> _handles = new HashSet<HandleBase>();
private readonly LinkedList<WeakReference<HandleBase>> _weakReferences = new LinkedList<WeakReference<HandleBase>>();
public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
{
@@ -210,6 +212,11 @@ namespace YooAsset
if (_steps == ESteps.ProcessBundleResult)
return false;
if (_resManager.UseWeakReferenceHandle)
{
TryCleanupWeakReference();
}
return RefCount <= 0;
}
@@ -222,7 +229,15 @@ namespace YooAsset
RefCount++;
HandleBase handle = HandleFactory.CreateHandle(this, typeof(T));
_handles.Add(handle);
if (_resManager.UseWeakReferenceHandle)
{
var weakRef = new WeakReference<HandleBase>(handle);
_weakReferences.AddLast(weakRef);
}
else
{
_handles.Add(handle);
}
return handle as T;
}
@@ -234,8 +249,16 @@ namespace YooAsset
if (RefCount <= 0)
throw new System.Exception("Should never get here !");
if (_handles.Remove(handle) == false)
throw new System.Exception("Should never get here !");
if (_resManager.UseWeakReferenceHandle)
{
if (RemoveWeakReference(handle) == false)
throw new System.Exception("Should never get here !");
}
else
{
if (_handles.Remove(handle) == false)
throw new System.Exception("Should never get here !");
}
// 引用计数减少
RefCount--;
@@ -246,10 +269,24 @@ namespace YooAsset
/// </summary>
public void ReleaseAllHandles()
{
List<HandleBase> tempers = _handles.ToList();
foreach (var handle in tempers)
if (_resManager.UseWeakReferenceHandle)
{
handle.Release();
List<WeakReference<HandleBase>> tempers = _weakReferences.ToList();
foreach (var weakRef in tempers)
{
if (weakRef.TryGetTarget(out HandleBase target))
{
target.Release();
}
}
}
else
{
List<HandleBase> tempers = _handles.ToList();
foreach (var handle in tempers)
{
handle.Release();
}
}
}
@@ -264,12 +301,29 @@ namespace YooAsset
// 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
// 注意:回调方法如果发生异常,会阻断列表里的后续回调方法!
List<HandleBase> tempers = _handles.ToList();
foreach (var hande in tempers)
if (_resManager.UseWeakReferenceHandle)
{
if (hande.IsValid)
List<WeakReference<HandleBase>> tempers = _weakReferences.ToList();
foreach (var weakRef in tempers)
{
hande.InvokeCallback();
if (weakRef.TryGetTarget(out HandleBase target))
{
if (target.IsValid)
{
target.InvokeCallback();
}
}
}
}
else
{
List<HandleBase> tempers = _handles.ToList();
foreach (var handle in tempers)
{
if (handle.IsValid)
{
handle.InvokeCallback();
}
}
}
}
@@ -294,6 +348,50 @@ namespace YooAsset
return status;
}
/// <summary>
/// 移除指定句柄的弱引用对象
/// </summary>
private bool RemoveWeakReference(HandleBase handle)
{
bool removed = false;
var currentNode = _weakReferences.First;
while (currentNode != null)
{
var nextNode = currentNode.Next;
if (currentNode.Value.TryGetTarget(out HandleBase target))
{
if (ReferenceEquals(target, handle))
{
_weakReferences.Remove(currentNode);
removed = true;
break;
}
}
currentNode = nextNode;
}
return removed;
}
/// <summary>
/// 清理所有失效的弱引用
/// </summary>
private void TryCleanupWeakReference()
{
var currentNode = _weakReferences.First;
while (currentNode != null)
{
var nextNode = currentNode.Next;
if (currentNode.Value.TryGetTarget(out HandleBase target) == false)
{
_weakReferences.Remove(currentNode);
// 引用计数减少
RefCount--;
}
currentNode = nextNode;
}
}
#region
/// <summary>
/// 出生的场景

View File

@@ -17,7 +17,7 @@ namespace YooAsset
AddChildOperation(_loadSubAssetsOp);
#if UNITY_WEBGL
if (_resManager.WebGLForceSyncLoadAsset())
if (_resManager.WebGLForceSyncLoadAsset)
_loadSubAssetsOp.WaitForAsyncComplete();
#endif
}

View File

@@ -15,7 +15,10 @@ namespace YooAsset
private long _sceneCreateIndex = 0;
private IBundleQuery _bundleQuery;
private int _bundleLoadingMaxConcurrency;
private bool _webGLForceSyncLoadAsset;
// 开发者配置选项
public bool WebGLForceSyncLoadAsset { private set; get; }
public bool UseWeakReferenceHandle { private set; get; }
/// <summary>
/// 所属包裹
@@ -44,7 +47,8 @@ namespace YooAsset
public void Initialize(InitializeParameters parameters, IBundleQuery bundleServices)
{
_bundleLoadingMaxConcurrency = parameters.BundleLoadingMaxConcurrency;
_webGLForceSyncLoadAsset = parameters.WebGLForceSyncLoadAsset;
WebGLForceSyncLoadAsset = parameters.WebGLForceSyncLoadAsset;
UseWeakReferenceHandle = parameters.UseWeakReferenceHandle;
_bundleQuery = bundleServices;
SceneManager.sceneUnloaded += OnSceneUnloaded;
}
@@ -331,10 +335,6 @@ namespace YooAsset
{
return BundleLoadingCounter >= _bundleLoadingMaxConcurrency;
}
internal bool WebGLForceSyncLoadAsset()
{
return _webGLForceSyncLoadAsset;
}
private LoadBundleFileOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo)
{