164 lines
4.6 KiB
C#
164 lines
4.6 KiB
C#
/*
|
|
* @Author: 白哉
|
|
* @Description:
|
|
* @Date: 2025年05月26日 星期一 11:05:13
|
|
* @Modify:
|
|
*/
|
|
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace Logic.Multilingual
|
|
{
|
|
public enum MultilingualType
|
|
{
|
|
None,
|
|
ZH,
|
|
TDZH,
|
|
EN,
|
|
JP,
|
|
KR,
|
|
}
|
|
|
|
|
|
public class MultilingualData : ScriptableObject
|
|
{
|
|
public List<MultilingualFontGroup> FontGroups = new List<MultilingualFontGroup>();
|
|
public List<MultilingualItem> Items = new List<MultilingualItem>();
|
|
public List<MultilingualType> TargetTypes = new List<MultilingualType>();
|
|
private Dictionary<uint, MultilingualItem> _itemDict;
|
|
public Dictionary<uint, MultilingualItem> ItemDict => _itemDict;
|
|
|
|
|
|
public string GetMultilingualStr(uint id, MultilingualType type)
|
|
{
|
|
if (_itemDict == null) RefreshDict();
|
|
if (_itemDict == null) return string.Empty;
|
|
if (!_itemDict.TryGetValue(id, out var item)) return string.Empty;
|
|
|
|
var ret = type switch
|
|
{
|
|
MultilingualType.ZH => item.ZH,
|
|
MultilingualType.TDZH => item.TDZH,
|
|
MultilingualType.EN => item.EN,
|
|
MultilingualType.JP => item.JP,
|
|
MultilingualType.KR => item.KR,
|
|
_ => string.Empty,
|
|
};
|
|
|
|
// TODO 临时换色
|
|
ret = ret.Replace( "<color=yellow>", "<color=orange>");
|
|
// TODO 临时转义换色
|
|
ret = ret.Replace( "**<", "<color=orange>");
|
|
ret = ret.Replace( ">**", "</color>");
|
|
|
|
return ret;
|
|
}
|
|
|
|
public TMP_FontAsset GetMultilingualFont(uint fontId, MultilingualType type)
|
|
{
|
|
if (fontId == 0) return null;
|
|
foreach (var group in FontGroups)
|
|
{
|
|
if (group.FontID != fontId) continue;
|
|
return type switch
|
|
{
|
|
MultilingualType.ZH => group.ZHFont,
|
|
MultilingualType.TDZH => group.TDZHFont,
|
|
MultilingualType.EN => group.ENFont,
|
|
MultilingualType.JP => group.JPFont,
|
|
MultilingualType.KR => group.KRFont,
|
|
_ => null,
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public uint GetFontGroupID(TMP_FontAsset font)
|
|
{
|
|
foreach (var group in FontGroups)
|
|
{
|
|
if (group.ZHFont == font) return group.FontID;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public void RefreshDict()
|
|
{
|
|
if (_itemDict == null) _itemDict = new Dictionary<uint, MultilingualItem>();
|
|
if (_itemDict.Count == Items.Count) return;
|
|
_itemDict.Clear();
|
|
foreach (var item in Items) _itemDict[item.ID] = item;
|
|
}
|
|
|
|
public MultilingualType GetSystemLanguageTargetMultilingual(MultilingualType type)
|
|
{
|
|
var index = (int)type - 1;
|
|
if (index >= TargetTypes.Count) return type;
|
|
return TargetTypes[index];
|
|
}
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class MultilingualItem
|
|
{
|
|
public uint ID;
|
|
public string ZH;
|
|
public string TDZH;
|
|
public string EN;
|
|
public string JP;
|
|
public string KR;
|
|
public bool IsProperNoun;
|
|
public bool IsDialogue;
|
|
public string DialogueSpeaker;
|
|
public bool IsDeprecated;
|
|
public bool IsCustom;
|
|
|
|
[NonSerialized]
|
|
public string Desc;
|
|
|
|
|
|
public MultilingualItem()
|
|
{
|
|
IsCustom = false;
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
ZH = ZH.Replace("\r\n", "\n");
|
|
TDZH = TDZH.Replace("\r\n", "\n");
|
|
EN = EN.Replace("\r\n", "\n");
|
|
JP = JP.Replace("\r\n", "\n");
|
|
KR = KR.Replace("\r\n", "\n");
|
|
DialogueSpeaker = DialogueSpeaker.Replace("\r\n", "\n");
|
|
if(!string.IsNullOrEmpty(Desc))Desc = Desc.Replace("\r\n", "\n");
|
|
}
|
|
|
|
// 不分大小写将 True 或者 False 字符串转换为布尔值
|
|
public static bool ParseBoolStr(string str)
|
|
{
|
|
return string.Equals(str, "True", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class MultilingualFontGroup
|
|
{
|
|
public uint FontID;
|
|
public TMP_FontAsset ZHFont;
|
|
public TMP_FontAsset TDZHFont;
|
|
public TMP_FontAsset ENFont;
|
|
public TMP_FontAsset JPFont;
|
|
public TMP_FontAsset KRFont;
|
|
}
|
|
} |