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

70 lines
2.0 KiB
C#
Raw Normal View History

2025-08-01 19:32:29 +08:00
using System;
using AlicizaX.Localization.Runtime;
using AlicizaX;
using Paps.UnityToolbarExtenderUIToolkit;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[MainToolbarElement("LocalizationDropdownField", alignment: ToolbarAlign.Right, order: 0)]
public class LocalizationDropdownField : IMGUIContainer
{
private static GUIContent appConfigBtContent;
private static string[] _languageTypeNames;
public void InitializeElement()
{
var nameArray = Enum.GetNames(typeof(Language));
_languageTypeNames = new string[nameArray.Length - 1];
for (int i = 1; i < nameArray.Length; i++)
{
var name = nameArray[i];
_languageTypeNames[i - 1] = name;
}
appConfigBtContent =
EditorGUIUtility.TrTextContentWithIcon("", "",
"Settings");
onGUIHandler = MyGUIMethod;
}
private void MyGUIMethod()
{
GUILayout.BeginHorizontal();
string title = _languageTypeNames[GetPrefsIndex()];
appConfigBtContent.text = title;
if (EditorGUILayout.DropdownButton(appConfigBtContent, FocusType.Passive, EditorStyles.toolbarPopup, GUILayout.MaxWidth(120)))
{
DrawEditorToolDropdownMenus();
}
GUILayout.Space(5);
GUILayout.EndHorizontal();
}
static void DrawEditorToolDropdownMenus()
{
int index = GetPrefsIndex();
GenericMenu popMenu = new GenericMenu();
for (int i = 0; i < _languageTypeNames.Length; i++)
{
var selected = index == i;
var toolAttr = _languageTypeNames[i];
popMenu.AddItem(new GUIContent(toolAttr), selected, menuIdx => { ClickToolsSubmenu((int)menuIdx); }, i);
}
popMenu.ShowAsContext();
}
static void ClickToolsSubmenu(int menuIdx)
{
EditorPrefs.SetInt(LocalizationComponent.PrefsKey, menuIdx + 1);
}
static int GetPrefsIndex()
{
return EditorPrefs.GetInt(LocalizationComponent.PrefsKey, 1) - 1;
}
}