67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using UnityEngine;
|
|
|
|
public static class PlayerTitleResolver
|
|
{
|
|
private const string PlayerTitleLibraryResourcePath = "so/player_titles/PlayerTitleLibrary";
|
|
|
|
private static PlayerTitleLibrary cachedTitleLibrary;
|
|
|
|
public readonly struct ResolvedTitle
|
|
{
|
|
public readonly string titleId;
|
|
public readonly string titleText;
|
|
public readonly Sprite titleSprite;
|
|
|
|
public bool HasSprite => titleSprite != null;
|
|
public bool HasText => !string.IsNullOrWhiteSpace(titleText);
|
|
public bool HasAny => HasSprite || HasText;
|
|
|
|
public ResolvedTitle(string titleId, string titleText, Sprite titleSprite)
|
|
{
|
|
this.titleId = titleId ?? string.Empty;
|
|
this.titleText = titleText ?? string.Empty;
|
|
this.titleSprite = titleSprite;
|
|
}
|
|
}
|
|
|
|
public static ResolvedTitle Resolve(string titleId, string fallbackText)
|
|
{
|
|
string safeTitleId = string.IsNullOrWhiteSpace(titleId) ? string.Empty : titleId.Trim();
|
|
string safeFallbackText = string.IsNullOrWhiteSpace(fallbackText) ? string.Empty : fallbackText.Trim();
|
|
Sprite sprite = null;
|
|
string resolvedText = safeFallbackText;
|
|
|
|
PlayerTitleLibrary library = GetTitleLibrary();
|
|
if (library != null && !string.IsNullOrEmpty(safeTitleId)
|
|
&& library.TryGetTitle(safeTitleId, out PlayerTitleLibrary.PlayerTitleEntry entry)
|
|
&& entry != null)
|
|
{
|
|
sprite = entry.titleSprite;
|
|
|
|
if (string.IsNullOrWhiteSpace(resolvedText))
|
|
{
|
|
resolvedText = !string.IsNullOrWhiteSpace(entry.chineseName)
|
|
? entry.chineseName.Trim()
|
|
: (!string.IsNullOrWhiteSpace(entry.englishName) ? entry.englishName.Trim() : safeTitleId);
|
|
}
|
|
}
|
|
|
|
return new ResolvedTitle(safeTitleId, resolvedText, sprite);
|
|
}
|
|
|
|
public static Sprite ResolveSprite(string titleId)
|
|
{
|
|
return Resolve(titleId, string.Empty).titleSprite;
|
|
}
|
|
|
|
public static PlayerTitleLibrary GetTitleLibrary()
|
|
{
|
|
if (cachedTitleLibrary == null)
|
|
{
|
|
cachedTitleLibrary = Resources.Load<PlayerTitleLibrary>(PlayerTitleLibraryResourcePath);
|
|
}
|
|
|
|
return cachedTitleLibrary;
|
|
}
|
|
}
|