When using Addressables to load a TMP_SpriteAsset, the loaded asset appears in the Inspector as a ScriptableObject instead of being properly recognized and applied as a TMP_SpriteAsset. As a result, the expected sprite functionality doesn’t work.
What could be causing this issue, and how can it be resolved?
I’m using Unity 2023.3, and I followed the proper steps for loading the asset, including ensuring that the path is correct and that the TMP_SpriteAsset exists at the specified location. Below is the code snippet for loading the asset:
public void Load_Addressable_SpriteAsset(string path, Action<TMP_SpriteAsset> onCompleted)
{
if (string.IsNullOrEmpty(path))
{
Debug.LogError("Path cannot be null or empty.");
onCompleted?.Invoke(null); // Return null to indicate failure
return;
}
if (onCompleted == null)
{
Debug.LogError("OnCompleted callback cannot be null.");
return;
}
Debug.Log($"Attempting to load TMP_SpriteAsset from path: {path}");
// Start the loading operation
var handle = Addressables.LoadAssetAsync<TMP_SpriteAsset>(path);
// Subscribe to the completion event
handle.Completed += operation =>
{
if (operation.Status == AsyncOperationStatus.Succeeded)
{
if (operation.Result != null)
{
Debug.Log($"Successfully loaded TMP_SpriteAsset: {operation.Result.name}");
onCompleted?.Invoke(operation.Result); // Pass the loaded asset to the callback
}
else
{
Debug.LogError($"TMP_SpriteAsset at path '{path}' is null.");
onCompleted?.Invoke(null);
}
}
else
{
Debug.LogError($"Failed to load TMP_SpriteAsset at path: {path}. Status: {operation.Status}");
onCompleted?.Invoke(null);
}
// Release the asset handle to free up memory
Addressables.Release(handle);
};
}