VanguardReMaster/client/Assets/Vanguard/Runtime/UI/CardImageService.cs

87 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Vanguard.UI
{
/// <summary>
/// Загрузка настоящих артов по imageKey. Соглашение: спрайт лежит в
/// Assets/Vanguard/Resources/CardArt/{imageKey}.png. Пока файла нет —
/// используется процедурный CardArt. Позже этот же вход можно
/// перевести на Addressables без изменения экранов.
/// </summary>
public static class CardImageService
{
private const string ResourceFolder = "CardArt/";
private static readonly Dictionary<string, Sprite> Cache =
new Dictionary<string, Sprite>();
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;
}
/// <summary>Спрайт реального арта или процедурный fallback.</summary>
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<Texture2D>(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;
}
/// <summary>
/// "BT01/042EN" → "bt01_042". Нужно, когда каталог ещё не загрузился,
/// а арт уже лежит в Resources/CardArt.
/// </summary>
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;
}
}
}