using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Vanguard.UI
{
///
/// Загрузка настоящих артов по imageKey. Соглашение: спрайт лежит в
/// Assets/Vanguard/Resources/CardArt/{imageKey}.png. Пока файла нет —
/// используется процедурный CardArt. Позже этот же вход можно
/// перевести на Addressables без изменения экранов.
///
public static class CardImageService
{
private const string ResourceFolder = "CardArt/";
private static readonly Dictionary Cache =
new Dictionary();
public static void Apply(Image target, string imageKey, Sprite fallback)
{
if (target == null)
return;
var sprite = Resolve(imageKey, fallback);
target.sprite = sprite;
target.color = Color.white;
target.preserveAspect = false;
}
/// Спрайт реального арта или процедурный fallback.
public static Sprite Resolve(string imageKey, Sprite fallback)
{
if (string.IsNullOrWhiteSpace(imageKey))
return fallback;
if (Cache.TryGetValue(imageKey, out var cached))
return cached != null ? cached : fallback;
var texture = Resources.Load(ResourceFolder + imageKey);
if (texture == null)
{
Cache[imageKey] = null;
return fallback;
}
var sprite = Sprite.Create(
texture,
new Rect(0f, 0f, texture.width, texture.height),
new Vector2(0.5f, 0.5f),
100f);
Cache[imageKey] = sprite;
return sprite;
}
}
}