70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[CreateAssetMenu(fileName = "PlayerTitleLibrary", menuName = "Bansonic/Player Title Library")]
|
|
public class PlayerTitleLibrary : ScriptableObject
|
|
{
|
|
[Serializable]
|
|
public class PlayerTitleEntry
|
|
{
|
|
public string titleId;
|
|
public Sprite titleSprite;
|
|
public string chineseName;
|
|
public string englishName;
|
|
}
|
|
|
|
[SerializeField] private List<PlayerTitleEntry> titles = new List<PlayerTitleEntry>();
|
|
|
|
private Dictionary<string, PlayerTitleEntry> lookup;
|
|
|
|
public bool TryGetTitle(string key, out PlayerTitleEntry entry)
|
|
{
|
|
EnsureLookup();
|
|
|
|
entry = null;
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return lookup.TryGetValue(NormalizeKey(key), out entry) && entry != null;
|
|
}
|
|
|
|
private void EnsureLookup()
|
|
{
|
|
if (lookup != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lookup = new Dictionary<string, PlayerTitleEntry>(StringComparer.OrdinalIgnoreCase);
|
|
for (int i = 0; i < titles.Count; i++)
|
|
{
|
|
PlayerTitleEntry entry = titles[i];
|
|
if (entry == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
AddLookup(entry.titleId, entry);
|
|
AddLookup(entry.chineseName, entry);
|
|
AddLookup(entry.englishName, entry);
|
|
}
|
|
}
|
|
|
|
private void AddLookup(string key, PlayerTitleEntry entry)
|
|
{
|
|
string normalized = NormalizeKey(key);
|
|
if (!string.IsNullOrEmpty(normalized) && !lookup.ContainsKey(normalized))
|
|
{
|
|
lookup.Add(normalized, entry);
|
|
}
|
|
}
|
|
|
|
private static string NormalizeKey(string key)
|
|
{
|
|
return string.IsNullOrWhiteSpace(key) ? string.Empty : key.Trim();
|
|
}
|
|
}
|