com.alicizax.unity.editor.e.../Editor/ToolBarExtension/LocalizationDropdownField.cs
2025-08-05 10:28:37 +08:00

70 lines
2.0 KiB
C#

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];
for (int i = 0; i < nameArray.Length; i++)
{
_languageTypeNames[i] = nameArray[i];
}
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);
Debug.Log(((Language)menuIdx).ToString());
}
static int GetPrefsIndex()
{
return EditorPrefs.GetInt(LocalizationComponent.PrefsKey, 1);
}
}