87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using AlicizaX.Localization.Editor;
|
|
using AlicizaX.Localization.Runtime;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace UnityEngine.UI
|
|
{
|
|
[CustomEditor(typeof(UXTextMeshPro), true)]
|
|
[CanEditMultipleObjects]
|
|
public class UXTextMeshProEditor : TMPro.EditorUtilities.TMP_EditorPanelUI
|
|
{
|
|
private SerializedProperty text;
|
|
private SerializedProperty localizationID;
|
|
private List<LocalizationMasterConfig.LanguageEntry> _entries;
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
text = serializedObject.FindProperty("m_text");
|
|
localizationID = serializedObject.FindProperty("m_localizationID");
|
|
base.OnEnable();
|
|
_entries = LocalizationMasterConfig.GetAllEntries();
|
|
}
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
serializedObject.Update();
|
|
bool isPlay = Application.isPlaying;
|
|
|
|
|
|
// 准备Key列表
|
|
string[] keys = new string[_entries.Count];
|
|
for (int i = 0; i < _entries.Count; i++)
|
|
{
|
|
keys[i] = _entries[i].ID;
|
|
}
|
|
|
|
// Key选择逻辑
|
|
EditorGUI.BeginDisabledGroup(isPlay);
|
|
{
|
|
int currentIndex = -1;
|
|
for (int i = 0; i < keys.Length; i++)
|
|
{
|
|
if (localizationID.stringValue == keys[i])
|
|
{
|
|
currentIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 显示警告如果Key不存在
|
|
if (currentIndex == -1 && !string.IsNullOrEmpty(localizationID.stringValue))
|
|
{
|
|
EditorGUILayout.HelpBox("当前Key在配置文件中不存在", MessageType.Warning);
|
|
}
|
|
|
|
// 绘制Key下拉菜单
|
|
int newIndex = EditorGUILayout.Popup("本地化Key", currentIndex, keys);
|
|
if (newIndex != currentIndex && newIndex >= 0)
|
|
{
|
|
localizationID.stringValue = keys[newIndex];
|
|
text.stringValue = _entries[newIndex].ChineseSimplified;
|
|
}
|
|
}
|
|
EditorGUI.EndDisabledGroup();
|
|
|
|
// 显示预览信息
|
|
EditorGUI.BeginDisabledGroup(true);
|
|
{
|
|
EditorGUILayout.PropertyField(localizationID);
|
|
if (!string.IsNullOrEmpty(localizationID.stringValue))
|
|
{
|
|
var entry = _entries.Find(e => e.ID == localizationID.stringValue);
|
|
if (entry != null)
|
|
{
|
|
EditorGUILayout.TextField("当前文本预览", entry.ChineseSimplified);
|
|
}
|
|
}
|
|
}
|
|
EditorGUI.EndDisabledGroup();
|
|
|
|
serializedObject.ApplyModifiedProperties();
|
|
base.OnInspectorGUI();
|
|
}
|
|
}
|
|
}
|