com.alicizax.unity.framework/Editor/Localization/LocalizationComponentInspector.cs

53 lines
2.1 KiB
C#
Raw Normal View History

2025-10-11 15:18:09 +08:00
using System;
using System.Linq;
using AlicizaX.Editor;
using AlicizaX.Localization.Runtime;
using UnityEditor;
namespace AlicizaX.Localization.Editor
{
[CustomEditor(typeof(LocalizationComponent))]
internal sealed class LocalizationComponentInspector : GameFrameworkInspector
{
private string[] _languageNames = new[] { "None" };
private SerializedProperty _language;
private int languageIndex = -1;
private void OnEnable()
{
_language = serializedObject.FindProperty("_language");
_languageNames = LocalizationConfiguration.Instance.LanguageTypeNames.ToArray();
var languageName = EditorPrefs.GetString(LocalizationComponent.PrefsKey, "None");
_language.stringValue = languageName;
serializedObject.ApplyModifiedProperties();
languageIndex = _languageNames.ToList().FindIndex(t => t.Equals(languageName));
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
LocalizationComponent t = (LocalizationComponent)target;
EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
{
if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
{
int index = UnityEditor.EditorPrefs.GetInt(LocalizationComponent.PrefsKey, 0);
int selectedIndex = EditorGUILayout.Popup("Language", index, _languageNames);
}
else
{
int selectedIndex = EditorGUILayout.Popup("Play Mode", languageIndex, _languageNames);
if (selectedIndex != languageIndex)
{
languageIndex = selectedIndex;
_language.stringValue = _languageNames[languageIndex];
EditorPrefs.SetString(LocalizationComponent.PrefsKey, _language.stringValue);
}
}
}
serializedObject.ApplyModifiedProperties();
}
}
}