Files
2026-09-21 12:15:57 +03:00

43 lines
1.6 KiB
C#

using System.ComponentModel;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.Util;
using UnityEngine.U2D;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
/// <summary>
/// Provides sprites from atlases
/// </summary>
[DisplayName("Sprites from Atlases Provider")]
public class AtlasSpriteProvider : ResourceProviderBase
{
/// <inheritdoc/>
public override void Provide(ProvideHandle providerInterface)
{
var atlas = providerInterface.GetDependency<SpriteAtlas>(0);
if (atlas == null)
{
providerInterface.Complete<Sprite>(null, false, new System.Exception($"Sprite atlas failed to load for location {providerInterface.Location.PrimaryKey}."));
return;
}
var key = providerInterface.ResourceManager.TransformInternalId(providerInterface.Location);
ResourceManagerConfig.ExtractKeyAndSubKey(key, out string mainKey, out string subKey);
string spriteKey = string.IsNullOrEmpty(subKey) ? mainKey : subKey;
var sprite = atlas.GetSprite(spriteKey);
providerInterface.Complete(sprite, sprite != null, sprite != null ? null : new System.Exception($"Sprite failed to load for location {providerInterface.Location.PrimaryKey}."));
}
/// <inheritdoc/>
public override void Release(IResourceLocation location, object obj)
{
if (obj is Sprite sprite)
#if UNITY_EDITOR
Object.DestroyImmediate(sprite);
#else
Object.Destroy(sprite);
#endif
}
}
}