From d959ad2d7c2ebd5757d83adde073498328cb5f96 Mon Sep 17 00:00:00 2001 From: wuwenbo Date: Fri, 27 Jun 2025 12:05:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E8=AF=AD=E8=A8=80li?= =?UTF-8?q?ststring=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataAssetsScript/PlayerDataAssets.cs | 6 +-- .../Logic/Editor/MultilingualEditorWindow.cs | 50 +++++++++++++------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/My project/Assets/Scripts/DataAssetsScript/PlayerDataAssets.cs b/My project/Assets/Scripts/DataAssetsScript/PlayerDataAssets.cs index b84a9948d..e1d3e715a 100644 --- a/My project/Assets/Scripts/DataAssetsScript/PlayerDataAssets.cs +++ b/My project/Assets/Scripts/DataAssetsScript/PlayerDataAssets.cs @@ -78,11 +78,11 @@ public class PlayerInfo public Color Color; public Sprite FlagIcon; [MultilingualField] - public List StartChatBubble = new List(); + public List StartChatBubble; [MultilingualField] - public List MeetChatBubble = new List(); + public List MeetChatBubble; [MultilingualField] - public List LoseChatBubble = new List(); + public List LoseChatBubble; PlayerInfo() { foreach (TechType t in System.Enum.GetValues(typeof(TechType))) diff --git a/My project/Assets/Scripts/Logic/Editor/MultilingualEditorWindow.cs b/My project/Assets/Scripts/Logic/Editor/MultilingualEditorWindow.cs index 886d2ab4c..61fbbb8fa 100644 --- a/My project/Assets/Scripts/Logic/Editor/MultilingualEditorWindow.cs +++ b/My project/Assets/Scripts/Logic/Editor/MultilingualEditorWindow.cs @@ -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(); 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 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),遍历元素 - 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); } }