Skip to content

Commit ed4afda

Browse files
committed
fix: fix set up changed key
1 parent 09f0e44 commit ed4afda

2 files changed

Lines changed: 124 additions & 58 deletions

File tree

Assets/Code/Localization/Editor/LocalizationEditorWindow.cs

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static void OpenWindow()
3333
[FoldoutGroup("Localization File")]
3434
[ShowInInspector, PropertyOrder(1)]
3535
[OnValueChanged("FilterEntries")]
36-
[LabelText("🔍 Search Key")]
36+
[LabelText("Search Key")]
3737
private string _searchKey = string.Empty;
3838

3939
[FoldoutGroup("Localization File")]
@@ -45,7 +45,7 @@ private static void OpenWindow()
4545

4646
[FoldoutGroup("Localization File", expanded: true)]
4747
[ShowInInspector, PropertyOrder(3)]
48-
[Button("💾 Save Current Language", ButtonSizes.Large), GUIColor(0.6f, 1f, 0.6f)]
48+
[Button("Save Current Language", ButtonSizes.Large), GUIColor(0.6f, 1f, 0.6f)]
4949
private void Save()
5050
{
5151
if (string.IsNullOrEmpty(_selectedLanguage)) return;
@@ -60,17 +60,17 @@ private void Save()
6060
File.WriteAllLines(path, lines);
6161
AssetDatabase.Refresh();
6262

63-
Debug.Log($"Saved language file: {path}");
63+
Debug.Log($"Saved language file: {path}");
6464
}
6565

6666
[FoldoutGroup("Localization File", expanded: true)]
6767
[ShowInInspector, PropertyOrder(4)]
68-
[Button("🗑 Delete Selected Language", ButtonSizes.Large), GUIColor(1f, 0.4f, 0.4f)]
68+
[Button("Delete Selected Language", ButtonSizes.Large), GUIColor(1f, 0.4f, 0.4f)]
6969
private void DeleteSelectedLanguage()
7070
{
7171
if (string.IsNullOrEmpty(_selectedLanguage))
7272
{
73-
Debug.LogWarning("No language selected to delete.");
73+
Debug.LogWarning("No language selected to delete.");
7474
return;
7575
}
7676

@@ -79,15 +79,15 @@ private void DeleteSelectedLanguage()
7979
{
8080
File.Delete(path);
8181
AssetDatabase.Refresh();
82-
Debug.Log($"🗑 Deleted language file: {path}");
82+
Debug.Log($"Deleted language file: {path}");
8383

8484
_selectedLanguage = null;
8585
_entries.Clear();
8686
_allEntries.Clear();
8787
}
8888
else
8989
{
90-
Debug.LogWarning($"File not found: {path}");
90+
Debug.LogWarning($"File not found: {path}");
9191
}
9292
}
9393

@@ -103,7 +103,7 @@ private void DeleteSelectedLanguage()
103103

104104
[FoldoutGroup("Create New Language TxT")]
105105
[ShowInInspector, PropertyOrder(2)]
106-
[Button("🆕 Create New Language From Base", ButtonSizes.Large), GUIColor(0.1f, 0.8f, 1f)]
106+
[Button("Create New Language From Base", ButtonSizes.Large), GUIColor(0.1f, 0.8f, 1f)]
107107
private void CreateNewLanguageFromBase()
108108
{
109109
string newLang = _NewLanguage.ToString();
@@ -125,7 +125,7 @@ private void CreateNewLanguageFromBase()
125125
File.Copy(basePath, targetPath);
126126
AssetDatabase.Refresh();
127127

128-
Debug.Log($"Created '{newLang}' from '{_BaseLanguage}'.");
128+
Debug.Log($"Created '{newLang}' from '{_BaseLanguage}'.");
129129
}
130130

131131
[FoldoutGroup("Find Localized Assets")]
@@ -134,7 +134,7 @@ private void CreateNewLanguageFromBase()
134134
private List<TMPAssetEntry> _localizersInAssets = new();
135135

136136
[FoldoutGroup("Find Localized Assets", expanded: true)]
137-
[Button("📁 Find All TMP_Localizers in Resources", ButtonSizes.Large), GUIColor(0.1f, 0.8f, 1f)]
137+
[Button("Find All TMP_Localizers in Resources", ButtonSizes.Large), GUIColor(0.1f, 0.8f, 1f)]
138138
private void FindTMPLocalizersInAssets()
139139
{
140140
_localizersInAssets.Clear();
@@ -146,7 +146,7 @@ private void FindTMPLocalizersInAssets()
146146
GameObject prefabRoot = PrefabUtility.LoadPrefabContents(path);
147147
if (prefabRoot == null)
148148
{
149-
Debug.LogWarning($"Failed to load prefab contents: {path}");
149+
Debug.LogWarning($"Failed to load prefab contents: {path}");
150150
continue;
151151
}
152152

@@ -156,75 +156,103 @@ private void FindTMPLocalizersInAssets()
156156
{
157157
string objName = localizer.gameObject.name;
158158
string parentName = localizer.transform.parent != null ? localizer.transform.parent.name : "(root)";
159-
string childName = localizer.transform.childCount > 0 ? localizer.transform.GetChild(0).name : "(no child)";
160159

161160
_localizersInAssets.Add(new TMPAssetEntry
162161
{
163162
AssetPath = path,
164163
GameObjectName = objName,
165164
ParentName = parentName,
166-
ChildName = childName,
167-
Component = localizer,
168165
LocalizationKey = localizer.localizationKey
169166
});
170167
}
171168

172169
PrefabUtility.UnloadPrefabContents(prefabRoot);
173170
}
174171

175-
Debug.Log($"Done! Total found TMP_Localizers in prefabs: {_localizersInAssets.Count}");
172+
Debug.Log($"Done! Total found TMP_Localizers in prefabs: {_localizersInAssets.Count}");
176173
}
177174

178175
[FoldoutGroup("Find Localized Assets")]
179-
[Button("💾 Save Changes To Assets", ButtonSizes.Large), GUIColor(0.6f, 1f, 0.6f)]
176+
[Button("Save Changes To Assets", ButtonSizes.Large), GUIColor(0.6f, 1f, 0.6f)]
180177
private void ApplyLocalizationKeyChangesToAssets()
181178
{
182-
var pathsToSave = new HashSet<string>();
179+
Debug.Log("=== 🔁 Start Applying LocalizationKey Changes ===");
180+
181+
int updatedCount = 0;
182+
var pathsToLog = new HashSet<string>();
183183

184184
foreach (var entry in _localizersInAssets)
185185
{
186-
if (entry.Component == null) continue;
186+
if (string.IsNullOrEmpty(entry.AssetPath) || string.IsNullOrEmpty(entry.GameObjectName))
187+
{
188+
Debug.LogWarning("⚠️ Missing path or object name, skipping.");
189+
continue;
190+
}
187191

188-
Undo.RecordObject(entry.Component, "Change Localization Key");
189-
entry.Component.localizationKey = entry.LocalizationKey;
190-
EditorUtility.SetDirty(entry.Component.gameObject);
192+
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(entry.AssetPath);
193+
if (prefab == null)
194+
{
195+
Debug.LogWarning($"⚠️ Failed to load prefab at path: {entry.AssetPath}");
196+
continue;
197+
}
191198

192-
if (!string.IsNullOrEmpty(entry.AssetPath))
199+
var localizers = prefab.GetComponentsInChildren<LocalizeBase>(true);
200+
foreach (var localizer in localizers)
193201
{
194-
pathsToSave.Add(entry.AssetPath);
202+
if (localizer.gameObject.name != entry.GameObjectName) continue;
203+
204+
string oldKey = localizer.localizationKey;
205+
string newKey = entry.LocalizationKey;
206+
207+
if (oldKey != newKey)
208+
{
209+
Undo.RecordObject(localizer, "Change Localization Key");
210+
localizer.localizationKey = newKey;
211+
EditorUtility.SetDirty(localizer);
212+
updatedCount++;
213+
214+
Debug.Log($"📝 Updated '{entry.GameObjectName}' in '{entry.AssetPath}': '{oldKey}' ➜ '{newKey}'");
215+
}
216+
else
217+
{
218+
Debug.Log($"✅ No change needed for '{entry.GameObjectName}' (key already '{oldKey}')");
219+
}
220+
221+
pathsToLog.Add(entry.AssetPath);
222+
break; // нашли нужный объект — идём дальше
195223
}
196-
}
197224

198-
foreach (var path in pathsToSave)
199-
{
200-
var prefabRoot = PrefabUtility.LoadPrefabContents(path);
201-
PrefabUtility.SaveAsPrefabAsset(prefabRoot, path);
202-
PrefabUtility.UnloadPrefabContents(prefabRoot);
225+
EditorUtility.SetDirty(prefab);
203226
}
204227

205228
AssetDatabase.SaveAssets();
206-
Debug.Log("✅ Localization keys in assets updated and prefabs saved.");
229+
AssetDatabase.Refresh();
230+
231+
Debug.Log($"🎉 Done! Updated {updatedCount} key(s), affected {pathsToLog.Count} prefab(s).");
207232
}
233+
208234

209235
[FoldoutGroup("Add Missing Localizers", expanded: true)]
210-
[Button("Add TMP_Localizer to All TMP_Text In Resources", ButtonSizes.Large), GUIColor(0.8f, 0.9f, 1f)]
236+
[Button("Add TMP_Localizer to All TMP_Text In Resources", ButtonSizes.Large), GUIColor(0.8f, 0.9f, 1f)]
211237
private void AddTMP_LocalizersToResources()
212238
{
213239
int addedCount = 0;
214240

215241
GameObject[] allPrefabs = Resources.LoadAll<GameObject>("");
216242
foreach (var prefab in allPrefabs)
217243
{
218-
if (prefab == null) continue;
244+
if (prefab == null)
245+
continue;
219246

220-
var path = AssetDatabase.GetAssetPath(prefab);
221-
if (string.IsNullOrEmpty(path)) continue;
247+
string path = AssetDatabase.GetAssetPath(prefab);
248+
if (string.IsNullOrEmpty(path))
249+
continue;
222250

223-
var root = PrefabUtility.LoadPrefabContents(path);
251+
GameObject root = PrefabUtility.LoadPrefabContents(path);
224252
bool wasModified = false;
225253

226-
var texts = root.GetComponentsInChildren<TMP_Text>(true);
227-
foreach (var text in texts)
254+
TMP_Text[] texts = root.GetComponentsInChildren<TMP_Text>(true);
255+
foreach (TMP_Text text in texts)
228256
{
229257
if (text.GetComponent<LocalizeBase>() == null)
230258
{
@@ -238,7 +266,7 @@ private void AddTMP_LocalizersToResources()
238266

239267
if (wasModified)
240268
{
241-
EditorUtility.SetDirty(root); // 💡 чтобы точно зафиксировалось
269+
EditorUtility.SetDirty(root);
242270
PrefabUtility.SaveAsPrefabAsset(root, path);
243271
}
244272

@@ -247,7 +275,7 @@ private void AddTMP_LocalizersToResources()
247275

248276
AssetDatabase.SaveAssets();
249277
AssetDatabase.Refresh();
250-
Debug.Log($"Added TMP_Localizer to {addedCount} TMP_Text objects.");
278+
Debug.Log($"Added TMP_Localizer to {addedCount} TMP_Text objects.");
251279
}
252280

253281
private void LoadLocalizationFile()
@@ -318,10 +346,10 @@ private IEnumerable<SystemLanguage> GetNewLanguages()
318346
[Serializable]
319347
public class LocalizationEntry
320348
{
321-
[HorizontalGroup("Row", Width = 250)]
349+
[HorizontalGroup("Key = Value", Width = 250)]
322350
public string Key;
323351

324-
[HorizontalGroup("Row")]
352+
[HorizontalGroup("Key = Value")]
325353
[MultiLineProperty(2)]
326354
public string Value;
327355
}
@@ -335,18 +363,12 @@ public class TMPAssetEntry
335363
[ReadOnly, TableColumnWidth(150)]
336364
public string ParentName;
337365

338-
[ReadOnly, TableColumnWidth(150)]
339-
public string ChildName;
340-
341366
[ReadOnly, TableColumnWidth(300)]
342367
public string AssetPath;
343368

344-
[HideInInspector]
345-
public LocalizeBase Component;
346-
347369
[TableColumnWidth(400, resizable: true)]
348370
public string LocalizationKey;
349371
}
350372
}
351373
}
352-
#endif
374+
#endif

0 commit comments

Comments
 (0)