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;
}
///
/// "BT01/042EN" → "bt01_042". Нужно, когда каталог ещё не загрузился,
/// а арт уже лежит в Resources/CardArt.
///
public static string ImageKeyForPrinting(string printingId)
{
if (string.IsNullOrEmpty(printingId))
return null;
var slash = printingId.IndexOf('/');
if (slash <= 0 || slash >= printingId.Length - 1)
return null;
var set = printingId.Substring(0, slash).ToLowerInvariant();
var tail = printingId.Substring(slash + 1);
var number = string.Empty;
for (var i = 0; i < tail.Length; i++)
{
var c = tail[i];
if (char.IsDigit(c) || (i == 0 && (c == 'S' || c == 's')))
{
number += char.ToLowerInvariant(c);
continue;
}
break;
}
return number.Length > 0 ? set + "_" + number : null;
}
}
}