com.alicizax.unity.editor.e.../Editor/ToolBarExtension/LocalizationDropdownField.cs

112 lines
3.3 KiB
C#
Raw Normal View History

2025-08-01 19:32:29 +08:00
using System;
2025-12-10 17:38:00 +08:00
using System.Collections.Generic;
2025-09-23 12:05:22 +08:00
using System.Linq;
2025-12-10 17:38:00 +08:00
using System.Reflection;
2025-08-01 19:32:29 +08:00
using AlicizaX.Localization.Runtime;
using AlicizaX;
2025-09-23 12:05:22 +08:00
using AlicizaX.Localization.Editor;
2025-08-01 19:32:29 +08:00
using Paps.UnityToolbarExtenderUIToolkit;
using UnityEditor;
2025-12-10 17:38:00 +08:00
using UnityEditor.SceneManagement;
2025-08-01 19:32:29 +08:00
using UnityEngine;
using UnityEngine.UIElements;
[MainToolbarElement("LocalizationDropdownField", alignment: ToolbarAlign.Right, order: 0)]
public class LocalizationDropdownField : IMGUIContainer
{
2025-12-01 16:45:54 +08:00
private GUIContent appConfigBtContent;
2025-08-01 19:32:29 +08:00
2025-12-01 16:45:54 +08:00
private string[] _languageTypeNames;
2025-08-01 19:32:29 +08:00
public void InitializeElement()
{
2025-09-23 12:05:22 +08:00
_languageTypeNames = LocalizationConfiguration.Instance.LanguageTypeNames.ToArray();
2025-08-01 19:32:29 +08:00
appConfigBtContent =
EditorGUIUtility.TrTextContentWithIcon("", "",
"Settings");
onGUIHandler = MyGUIMethod;
}
private void MyGUIMethod()
{
GUILayout.BeginHorizontal();
2025-09-23 12:05:22 +08:00
string title = GetPrefsStr();
2025-08-01 19:32:29 +08:00
appConfigBtContent.text = title;
if (EditorGUILayout.DropdownButton(appConfigBtContent, FocusType.Passive, EditorStyles.toolbarPopup, GUILayout.MaxWidth(120)))
{
DrawEditorToolDropdownMenus();
}
GUILayout.Space(5);
GUILayout.EndHorizontal();
}
2025-12-01 16:45:54 +08:00
void DrawEditorToolDropdownMenus()
2025-08-01 19:32:29 +08:00
{
2025-09-23 12:05:22 +08:00
string langaugeName = GetPrefsStr();
2025-08-01 19:32:29 +08:00
GenericMenu popMenu = new GenericMenu();
for (int i = 0; i < _languageTypeNames.Length; i++)
{
var toolAttr = _languageTypeNames[i];
2025-09-23 12:05:22 +08:00
var selected = langaugeName == toolAttr;
popMenu.AddItem(new GUIContent(toolAttr), selected, menuIdx => { ClickToolsSubmenu(toolAttr); }, toolAttr);
2025-08-01 19:32:29 +08:00
}
popMenu.ShowAsContext();
}
2025-12-01 16:45:54 +08:00
void ClickToolsSubmenu(string langaugeName)
2025-08-01 19:32:29 +08:00
{
2025-09-23 12:05:22 +08:00
EditorPrefs.SetString(LocalizationComponent.PrefsKey, langaugeName);
2025-12-10 17:38:00 +08:00
InvokeOnValidateInScene();
2025-09-23 12:05:22 +08:00
Debug.Log(langaugeName);
2025-08-01 19:32:29 +08:00
}
2025-12-01 16:45:54 +08:00
string GetPrefsStr()
2025-08-01 19:32:29 +08:00
{
2025-09-23 12:05:22 +08:00
return EditorPrefs.GetString(LocalizationComponent.PrefsKey, "None");
2025-08-01 19:32:29 +08:00
}
2025-12-10 17:38:00 +08:00
public static void InvokeOnValidateInScene()
{
System.Type targetType = AlicizaX.Utility.Assembly.GetType("UnityEngine.UI.UXTextMeshPro");
MethodInfo onValidateMethod = targetType.GetMethod("OnValidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (onValidateMethod == null)
{
Debug.LogWarning($"类型 {targetType.Name} 没有找到 OnValidate 方法(可能是用其他签名或不存在)");
return;
}
List<Component> components = new List<Component>();
components = GameObject.FindObjectsOfType(targetType).ToList().Select(t => (t as Component)).ToList();
if (components.Count == 0)
{
Debug.Log("未在场景中找到任何目标组件。");
return;
}
int count = 0;
var scenesToMarkDirty = EditorSceneManager.GetActiveScene();
foreach (var comp in components)
{
Undo.RecordObject(comp, "Invoke OnValidate");
onValidateMethod.Invoke(comp, null);
EditorUtility.SetDirty(comp);
count++;
}
if (count > 0)
{
EditorSceneManager.MarkSceneDirty(scenesToMarkDirty);
}
}
2025-08-01 19:32:29 +08:00
}