【Addressable】关于开启ProfilerEvents后导致自动清理缓存报错问题

关于开启事件收集导致清理缓存报错异常问题

版本1.19.19
报错信息:

Exception: Attempting to use an invalid operation handle
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.get_InternalOp () (at Library/PackageCache/com.unity.addressables@1.19.19/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:453)
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.GetDependencies (System.Collections.Generic.List`1[T] deps) (at Library/PackageCache/com.unity.addressables@1.19.19/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:436)
UnityEngine.AddressableAssets.Utility.ResourceManagerDiagnostics.SumDependencyNameHashCodes (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle handle) (at Library/PackageCache/com.unity.addressables@1.19.19/Runtime/Utility/ResourceManagerEventCollector.cs:43)
UnityEngine.AddressableAssets.Utility.ResourceManagerDiagnostics.SumDependencyNameHashCodes (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle handle
Unity Addressables 资源加载过程中,若出现 `System.UriFormatException: Invalid URI: Invalid port specified` 异常,通常表示构造的 URI 字符串中端口号格式不合法或包含非法字符。Addressables 系统依赖于标准的 .NET `Uri` 类进行资源路径解析,因此其 URI 格式必须严格符合规范。 ### URI 端口格式要求 URI 中的端口号必须是 0 到 65535 之间的整数。若在 Addressables 配置或运行时构造的 URI 中包含非整数端口号(如 `http://10.0.100.76:HostingServicePort/catalog_1.0.6.a.hash`),将导致 `UriFormatException` 抛出。此处的 `HostingServicePort` 显然不是有效的端口号,而是一个占位符或变量名,未能在运行时被正确替换为整数[^1]。 ### 解决方案 确保 Addressables 使用的远程资源路径中端口号为有效整数。如果使用变量动态设置端口号,应在构建 URI 前进行类型检查和范围验证: ```csharp int port = 8080; // 确保 port 是合法的整数 string baseUri = $"http://10.0.100.76:{port}/catalog_1.0.6.a.hash"; ``` 在加载远程资源时,使用 `Addressables.LoadAssetAsync<T>` 并确保传入的地址为合法 URI: ```csharp string uriString = $"http://10.0.100.76:{port}/catalog_1.0.6.a.hash"; if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute)) { var handle = Addressables.LoadAssetAsync<GameObject>(uriString); handle.Completed += (op) => { if (op.Status == AsyncOperationStatus.Succeeded) { Instantiate(op.Result); } }; } else { Debug.LogError("Invalid URI format for Addressables load."); } ``` ### 自定义端口验证逻辑 为避免运行时异常,可以在 Addressables 初始化或资源加载前加入端口有效性验证逻辑: ```csharp public static bool IsValidPort(int port) { return port >= 0 && port <= 65535; } ``` ### 异常处理机制 在 Addressables 资源加载过程中,建议结合 `try-catch` 块捕获 `UriFormatException`,以提供更友好的错误提示: ```csharp try { string uriString = $"http://10.0.100.76:{invalidPort}/catalog_1.0.6.a.hash"; var handle = Addressables.LoadAssetAsync<GameObject>(uriString); } catch (UriFormatException ex) { Debug.LogError($"Invalid URI format when loading Addressables asset: {ex.Message}"); } ``` ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值