增加多语言liststring的支持

This commit is contained in:
wuwenbo 2025-06-27 12:05:19 +08:00
parent 7e3b9e9f6c
commit d959ad2d7c
2 changed files with 39 additions and 17 deletions

View File

@ -78,11 +78,11 @@ public class PlayerInfo
public Color Color;
public Sprite FlagIcon;
[MultilingualField]
public List<string> StartChatBubble = new List<string>();
public List<string> StartChatBubble;
[MultilingualField]
public List<string> MeetChatBubble = new List<string>();
public List<string> MeetChatBubble;
[MultilingualField]
public List<string> LoseChatBubble = new List<string>();
public List<string> LoseChatBubble;
PlayerInfo()
{
foreach (TechType t in System.Enum.GetValues(typeof(TechType)))

View File

@ -386,15 +386,18 @@ namespace Logic.Editor
var fields = asset.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
var value = field.GetValue(asset);
var attr = field.GetCustomAttribute<MultilingualFieldAttribute>();
if (attr != null)
{
var str = ((string)field.GetValue(asset)).Trim().Replace("\r\n", "\n");
if (!string.IsNullOrEmpty(str))
if (value is string s)
{
if (_zhStrDict.ContainsKey(str))
var str = s.Trim().Replace("\r\n", "\n");
if (string.IsNullOrEmpty(str)) continue;
if (_zhStrDict.TryGetValue(str, out var id))
{
field.SetValue(asset, _zhStrDict[str].ToString());
field.SetValue(asset, id.ToString());
}
else
{
@ -402,14 +405,36 @@ namespace Logic.Editor
field.SetValue(asset, _zhStrDict[str].ToString());
_idIndex++;
}
continue;
}
if (value is List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
var str = list[i].Trim().Replace("\r\n", "\n");
if (string.IsNullOrEmpty(str)) continue;
if (_zhStrDict.TryGetValue(str, out var id))
{
list[i] = id.ToString();
}
else
{
_zhStrDict[str] = _idIndex;
list[i] = _zhStrDict[str].ToString();
_idIndex++;
}
}
field.SetValue(asset, list);
continue;
}
}
var son = field.GetValue(asset);
if (son == null) continue;
// 如果是集合(如 List<T>),遍历元素
if (son is IEnumerable enumerable && !(son is string))
if (value == null) continue;
if (value is IEnumerable enumerable)
{
foreach (object item in enumerable)
{
@ -417,10 +442,7 @@ namespace Logic.Editor
}
}
// 如果是自定义对象(非基础类型),递归处理
else if (!son.GetType().IsPrimitive && son.GetType() != typeof(string))
{
TraverseObject(son);
}
else if (!value.GetType().IsPrimitive) TraverseObject(value);
}
}