I’m testing our addressable system and want to deal with error handling nicely obviously for when a key doesn’t exist should a typo creep in somewhere but it seems that the system throws this exception before I get a chance to deal with it…? Even wrapping in try/catch it never catches where I’m expecting it to…?
async void Start() {
try {
var loader = Addressables.LoadAssetAsync<Sprite>("invalid key");
await loader.Task;
} catch (System.Exception ex) {
Debug.Log("Why don't we get this? " + ex.Message);
}
}
This results in two exceptions…
Exception encountered in operation UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[UnityEngine.Sprite], result='', status='Failed': Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=invalid key
Exception encountered in operation UnityEngine.AddressableAssets.Initialization.InitializationOperation, result='', status='Succeeded' - Chain<Sprite>: ChainOperation of Type: UnityEngine.Sprite failed because dependent operation failed
Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=invalid key
I’ve found one other thread from months ago mentioning this but how is no-one else having this issue?? Is there some additional way to check the asset exists before attempting to load that I’m missing?
We just trawled through Addressables.ResourceLocators and cached off the asset once loaded, we do a check again our handle to see if it is loaded or not
So I’ve managed to work out this solution but it feels way too verbose that it needs to be…
var validateAddress = Addressables.LoadResourceLocationsAsync(address);
await validateAddress.Task;
if (validateAddress.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded) {
if (validateAddress.Result.Count > 0) {
// asset exists go ahead and load
}
}
Please, help. Get InvalidKeyException, use default assets for settings. Analyze tells no errors, use Default Build Script, addressable is under packed Assets group.
code
Code for load / Instantiate:
var validateKeyAsync = Addressables.LoadResourceLocationsAsync(key);
validateKeyAsync.Completed += validateAsync =>
{
if (validateAsync.Status != AsyncOperationStatus.Succeeded)
{
Debug.LogError($"[ Spawn Failed ] '{key}' invalid!");
return;
}
if (validateAsync.Result.Count <= 0)
{
Debug.LogError($"[ Spawn Failed ] address '{key}' results.Count == 0!");
return;
}
Addressables.LoadAssetAsync<GameObject>(key).Completed += laodAsync =>
{
if (laodAsync.Status != AsyncOperationStatus.Succeeded || !laodAsync.Result) return;
Addressables.InstantiateAsync(TextUtility.AddressById(data.id),
new InstantiationParameters(pos, Quaternion.identity, null)).Completed += instantiateAsync =>
{
if (instantiateAsync.Status != AsyncOperationStatus.Succeeded) return;
var go = instantiateAsync.Result;
var item = go.GetComponent<Item>();
item.data = data;
item.transform.position = pos;
};
};
};
validateAsync.Result.Count is always 0. If I comment this check out, I get InvalidKeyException. Prefab Is simple cube (ItemScript, Rigidbody, Collider, MeshRederer)
Works only in AssetDatabase mode. Simulate groups / use existing modes not work