Addressables 加载资源出错

最近使用Unity做项目,接入Addressables插件进行资源加载,当加载 继承 ScriptableObject 创建的 .asset 文件时,总是报错,加载不上。
报错信息如下:
UnityEngine.AddressableAssets.InvalidKeyException: Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown. No Location found for Key=FontConfig/FontConfig
System.Diagnostics.StackTrace:init_frames(Int32, Boolean)
UnityEngine.StackTraceUtility:ExtractStackTrace()
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.AddressableAssets.AddressablesImpl:LogException(AsyncOperationHandle, Exception)
System.Action2:Invoke(T1, T2) UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase1:set_OperationException(Exception)
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase1:Complete(TObject, Boolean, Exception, Boolean) UnityEngine.ResourceManagement.CompletedOperation1:Execute()
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase1:InvokeExecute() UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase1:Start(ResourceManager, AsyncOperationHandle, DelegateList`1)
UnityEngine.ResourceManagement.ResourceManager:StartOperation(AsyncOperationBa

加载代码如下:
IEnumerator LoadDll()
{
var handler = Addressables.LoadAssetAsync("FontConfig/FontConfig");
yield return handler;

    if (handler.Status == AsyncOperationStatus.Succeeded)
    {
        Debug.Log("load Succeed");
    }
    
}

请问这样加载错在哪里?正确的应该怎样去加载?

根据报错信息看,主要问题是Addressables无法根据指定的key去找到对应的资源。
加载Addressables资源,主要分以下几步:

  1. 在Addressables系统中注册资源,会生成一个address和key的映射。这通常是在地址配置表或组配置中完成的。
  2. 使用key去异步加载资源,比如你示例代码中的Addressables.LoadAssetAsync。
  3. Addressables根据key查找对应的address,然后基于address去实际加载资源。
    所以你需要检查的有以下几点:
  4. 确保资源已被正确注册到Addressables系统,并且Addressables可以根据指定的key找到正确的address。可以在Addressables窗口中查看。
  5. key的名称是否正确。区分大小写。
  6. 资源是否处于Addressable资源的加载路径之下。
  7. 资源的导入设置是否正确,确保资源可被外部程序访问。
  8. 检查资源本身是否正常,没有损坏或丢失。
  9. 如果确保以上都没问题,可以试试rebuildAddressables,重新构建Addressable系统。
    总结一下就是先确保key-address映射正确,然后确保资源可访问,最后强制rebuildAddressables试试。
    另外加载时也可以直接使用address去加载,可以避免key找不到资源的问题。