diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index b3ef0b09..387a6b62 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -52,15 +52,6 @@ namespace YooAsset /// WebGL平台强制同步加载资源对象 /// public bool WebGLForceSyncLoadAsset = false; - -#if YOOASSET_EXPERIMENTAL - /// - /// 启用弱引用资源句柄 - /// - public bool UseWeakReferenceHandle = false; -#else - internal bool UseWeakReferenceHandle = false; -#endif } /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs index b3ce65c3..719ea425 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs @@ -83,7 +83,6 @@ namespace YooAsset private readonly LoadBundleFileOperation _mainBundleLoader; private readonly List _bundleLoaders = new List(10); private readonly HashSet _handles = new HashSet(); - private readonly LinkedList> _weakReferences = new LinkedList>(); public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo) { @@ -230,11 +229,6 @@ namespace YooAsset if (IsLoading) return false; - if (_resManager.UseWeakReferenceHandle) - { - TryCleanupWeakReference(); - } - return RefCount <= 0; } @@ -247,15 +241,7 @@ namespace YooAsset RefCount++; HandleBase handle = HandleFactory.CreateHandle(this, typeof(T)); - if (_resManager.UseWeakReferenceHandle) - { - var weakRef = new WeakReference(handle); - _weakReferences.AddLast(weakRef); - } - else - { - _handles.Add(handle); - } + _handles.Add(handle); return handle as T; } @@ -267,17 +253,8 @@ namespace YooAsset if (RefCount <= 0) throw new YooInternalException($"Attempting to release handle when RefCount is already zero. Asset : {MainAssetInfo.AssetPath}"); - if (_resManager.UseWeakReferenceHandle) - { - // TODO 高危风险:如果移除弱引用失败,会导致资源永远无法释放。 - if (RemoveWeakReference(handle) == false) - throw new YooInternalException($"Handle not found in weak reference list. Asset: {MainAssetInfo.AssetPath}"); - } - else - { - if (_handles.Remove(handle) == false) - throw new YooInternalException($"Handle not found in cache list. Asset: {MainAssetInfo.AssetPath}"); - } + if (_handles.Remove(handle) == false) + throw new YooInternalException($"Handle not found in cache list. Asset: {MainAssetInfo.AssetPath}"); // 引用计数减少 RefCount--; @@ -288,24 +265,10 @@ namespace YooAsset /// public void ReleaseAllHandles() { - if (_resManager.UseWeakReferenceHandle) + List tempers = _handles.ToList(); + foreach (var handle in tempers) { - List> tempers = _weakReferences.ToList(); - foreach (var weakRef in tempers) - { - if (weakRef.TryGetTarget(out HandleBase target)) - { - target.Release(); - } - } - } - else - { - List tempers = _handles.ToList(); - foreach (var handle in tempers) - { - handle.Release(); - } + handle.Release(); } } @@ -330,43 +293,18 @@ namespace YooAsset Status = status; // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 - // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法! - if (_resManager.UseWeakReferenceHandle) + List tempers = _handles.ToList(); + foreach (var handle in tempers) { - List> tempers = _weakReferences.ToList(); - foreach (var weakRef in tempers) + if (handle.IsValid) { - if (weakRef.TryGetTarget(out HandleBase handle)) + try { - if (handle.IsValid) - { - try - { - handle.InvokeCallback(); - } - catch (Exception ex) - { - YooLogger.Error($"Exception in completion callback: {ex}"); - } - } + handle.InvokeCallback(); } - } - } - else - { - List tempers = _handles.ToList(); - foreach (var handle in tempers) - { - if (handle.IsValid) + catch (Exception ex) { - try - { - handle.InvokeCallback(); - } - catch (Exception ex) - { - YooLogger.Error($"Exception in completion callback: {ex}"); - } + YooLogger.Error($"Exception in completion callback: {ex}"); } } } @@ -392,50 +330,6 @@ namespace YooAsset return status; } - /// - /// 移除指定句柄的弱引用对象 - /// - 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; - } - - /// - /// 清理所有失效的弱引用 - /// - 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 调试信息 /// /// 出生的场景 diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index 69cde4fc..dd49cc96 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -19,7 +19,6 @@ namespace YooAsset // 开发者配置选项 public bool AutoUnloadBundleWhenUnused { private set; get; } public bool WebGLForceSyncLoadAsset { private set; get; } - public bool UseWeakReferenceHandle { private set; get; } /// /// 所属包裹 @@ -50,7 +49,6 @@ namespace YooAsset _bundleLoadingMaxConcurrency = parameters.BundleLoadingMaxConcurrency; AutoUnloadBundleWhenUnused = parameters.AutoUnloadBundleWhenUnused; WebGLForceSyncLoadAsset = parameters.WebGLForceSyncLoadAsset; - UseWeakReferenceHandle = parameters.UseWeakReferenceHandle; _bundleQuery = bundleServices; SceneManager.sceneUnloaded += OnSceneUnloaded; }