using System.Collections.Generic;
|
using UnityEditor;
|
using UnityEngine;
|
|
[CustomEditor(typeof(TextLanguageAdapter), true)]
|
[CanEditMultipleObjects]
|
public class TextLanguageAdapterEditor : Editor
|
{
|
private Dictionary<string, bool> m_FoldoutStates = new Dictionary<string, bool>();
|
|
private void OnEnable()
|
{
|
TextLanguageAdapterHelper.Initialize();
|
|
if (target is TextLanguageAdapter adapter)
|
{
|
if (!adapter.HasConfig(TextLanguageAdapter.DefaultLangId))
|
{
|
adapter.ReadCurrentToConfig(TextLanguageAdapter.DefaultLangId);
|
EditorUtility.SetDirty(adapter);
|
}
|
foreach (var langId in adapter.GetConfiguredLanguages()) m_FoldoutStates.TryAdd(langId, false);
|
}
|
}
|
|
public override void OnInspectorGUI()
|
{
|
var adapter = target as TextLanguageAdapter;
|
|
DrawBasicInfo(adapter);
|
EditorGUILayout.Space();
|
DrawLanguageConfigs(adapter);
|
|
if (GUI.changed) EditorUtility.SetDirty(target);
|
}
|
|
private void DrawBasicInfo(TextLanguageAdapter adapter)
|
{
|
using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
|
{
|
EditorGUILayout.LabelField("基本信息", EditorStyles.boldLabel);
|
|
EditorGUI.BeginChangeCheck();
|
|
// 允许手动拖拽目标组件和设置类型
|
Component newTarget = (Component)EditorGUILayout.ObjectField("目标组件", adapter.TargetTextComponent, typeof(Component), true);
|
TextComponentType newType = (TextComponentType)EditorGUILayout.EnumPopup("组件类型", adapter.TargetTextType);
|
|
if (EditorGUI.EndChangeCheck())
|
{
|
Undo.RecordObject(adapter, "Update Basic Info");
|
|
// 智能辅助:当用户拖拽新组件时,尝试自动匹配一下类型,但也允许手动覆盖
|
if (newTarget != null && newTarget != adapter.TargetTextComponent)
|
{
|
string typeName = newTarget.GetType().Name;
|
if (typeName == "GradientText") newType = TextComponentType.GradientText;
|
else if (typeName == "TextEx") newType = TextComponentType.TextEx;
|
else if (newTarget is UnityEngine.UI.Text) newType = TextComponentType.Text;
|
}
|
|
adapter.TargetTextComponent = newTarget;
|
adapter.TargetTextType = newType;
|
}
|
|
EditorGUILayout.HelpBox("请手动拖拽要适配的文本组件,并确认组件类型是否正确。", MessageType.Info);
|
}
|
}
|
|
private void DrawLanguageConfigs(TextLanguageAdapter adapter)
|
{
|
using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
|
{
|
EditorGUILayout.LabelField("语言排版配置", EditorStyles.boldLabel);
|
EditorGUILayout.HelpBox("default仅用于编辑时恢复初始状态,无对应语言配置时用组件当前状态", MessageType.Info);
|
|
foreach (var langId in adapter.GetConfiguredLanguages()) DrawLanguageConfigItem(adapter, langId);
|
|
EditorGUILayout.Space();
|
DrawAddLanguageConfig(adapter);
|
}
|
}
|
|
private void DrawLanguageConfigItem(TextLanguageAdapter adapter, string langId)
|
{
|
m_FoldoutStates.TryAdd(langId, false);
|
|
int presetIndex = System.Array.IndexOf(TextLanguageAdapterHelper.PresetLanguageIds, langId);
|
string name = presetIndex >= 0 ? TextLanguageAdapterHelper.PresetLanguageNames[presetIndex] : TextLanguageAdapter.GetLanguageShowName(langId);
|
string label = string.IsNullOrEmpty(name) ? langId : $"{langId} ({name})";
|
|
EditorGUI.indentLevel++;
|
if (langId == TextLanguageAdapter.DefaultLangId) GUI.backgroundColor = new Color(1f, 0.9f, 0.7f);
|
|
m_FoldoutStates[langId] = EditorGUILayout.Foldout(m_FoldoutStates[langId], label, true);
|
GUI.backgroundColor = Color.white;
|
|
if (m_FoldoutStates[langId])
|
{
|
EditorGUI.indentLevel++;
|
var config = adapter.LanguageConfigs.Get(langId);
|
if (config != null)
|
{
|
DrawConfigItem(config);
|
EditorGUILayout.Space();
|
DrawConfigItemActions(adapter, langId);
|
}
|
EditorGUI.indentLevel--;
|
}
|
EditorGUI.indentLevel--;
|
}
|
|
private void DrawConfigItem(LanguageConfigItem cfg)
|
{
|
EditorGUILayout.LabelField("RectTransform 配置", EditorStyles.miniBoldLabel);
|
EditorGUI.BeginChangeCheck();
|
|
cfg.anchoredPosition = EditorGUILayout.Vector2Field("Anchored Position", cfg.anchoredPosition);
|
cfg.sizeDelta = EditorGUILayout.Vector2Field("Size Delta", cfg.sizeDelta);
|
cfg.anchorMin = EditorGUILayout.Vector2Field("Anchor Min", cfg.anchorMin);
|
cfg.anchorMax = EditorGUILayout.Vector2Field("Anchor Max", cfg.anchorMax);
|
cfg.pivot = EditorGUILayout.Vector2Field("Pivot", cfg.pivot);
|
cfg.localScale = EditorGUILayout.Vector3Field("Local Scale", cfg.localScale);
|
cfg.localRotation = EditorGUILayout.Vector3Field("Local Rotation", cfg.localRotation);
|
|
EditorGUILayout.Space();
|
EditorGUILayout.LabelField("文本排版与适配配置", EditorStyles.miniBoldLabel);
|
cfg.font = (Font)EditorGUILayout.ObjectField("Font", cfg.font, typeof(Font), false);
|
cfg.fontStyle = (FontStyle)EditorGUILayout.EnumPopup("Font Style", cfg.fontStyle);
|
cfg.fontSize = EditorGUILayout.IntField("Font Size", cfg.fontSize);
|
cfg.lineSpacing = EditorGUILayout.FloatField("Line Spacing", cfg.lineSpacing);
|
cfg.alignment = (TextAnchor)EditorGUILayout.EnumPopup("Alignment", cfg.alignment);
|
cfg.alignByGeometry = EditorGUILayout.Toggle("Align By Geometry", cfg.alignByGeometry);
|
cfg.horizontalOverflow = (HorizontalWrapMode)EditorGUILayout.EnumPopup("Horizontal Overflow", cfg.horizontalOverflow);
|
cfg.verticalOverflow = (VerticalWrapMode)EditorGUILayout.EnumPopup("Vertical Overflow", cfg.verticalOverflow);
|
cfg.resizeTextForBestFit = EditorGUILayout.Toggle("Best Fit", cfg.resizeTextForBestFit);
|
|
if (cfg.resizeTextForBestFit)
|
{
|
EditorGUI.indentLevel++;
|
cfg.resizeTextMinSize = EditorGUILayout.IntField("Min Size", cfg.resizeTextMinSize);
|
cfg.resizeTextMaxSize = EditorGUILayout.IntField("Max Size", cfg.resizeTextMaxSize);
|
EditorGUI.indentLevel--;
|
}
|
|
if (EditorGUI.EndChangeCheck()) EditorUtility.SetDirty(target);
|
}
|
|
private void DrawConfigItemActions(TextLanguageAdapter adapter, string langId)
|
{
|
using (new EditorGUILayout.HorizontalScope())
|
{
|
GUILayout.Space(EditorGUI.indentLevel * 15f);
|
|
if (GUILayout.Button("从当前读取", GUILayout.Width(80)))
|
{
|
Undo.RecordObject(adapter, "Read Current Config");
|
adapter.ReadCurrentToConfig(langId);
|
}
|
|
if (GUILayout.Button("应用", GUILayout.Width(60)))
|
{
|
Undo.RecordObject(adapter, "Apply Config");
|
adapter.ApplyConfig(langId);
|
}
|
|
using (new EditorGUI.DisabledScope(langId == TextLanguageAdapter.DefaultLangId))
|
{
|
if (GUILayout.Button("删除", GUILayout.Width(50)))
|
{
|
Undo.RecordObject(adapter, "Remove Config");
|
adapter.RemoveConfig(langId);
|
m_FoldoutStates.Remove(langId);
|
}
|
}
|
}
|
}
|
|
private void DrawAddLanguageConfig(TextLanguageAdapter adapter)
|
{
|
EditorGUILayout.BeginHorizontal();
|
GUILayout.Space(15f);
|
EditorGUILayout.LabelField("添加预设语言:", GUILayout.Width(100));
|
|
float viewWidth = EditorGUIUtility.currentViewWidth - 30f;
|
float currentWidth = 115f;
|
float buttonWidth = 64f;
|
|
for (int i = 0; i < TextLanguageAdapterHelper.PresetLanguageIds.Length; i++)
|
{
|
string langId = TextLanguageAdapterHelper.PresetLanguageIds[i];
|
if (!adapter.HasConfig(langId))
|
{
|
if (currentWidth + buttonWidth > viewWidth)
|
{
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
GUILayout.Space(115f);
|
currentWidth = 115f;
|
}
|
|
if (GUILayout.Button(TextLanguageAdapterHelper.PresetLanguageNames[i], GUILayout.Width(60)))
|
{
|
Undo.RecordObject(adapter, "Add Language Config");
|
var newConfig = adapter.HasConfig(TextLanguageAdapter.DefaultLangId)
|
? adapter.LanguageConfigs.Get(TextLanguageAdapter.DefaultLangId).Clone()
|
: new LanguageConfigItem();
|
adapter.SetConfig(langId, newConfig);
|
m_FoldoutStates[langId] = true;
|
}
|
currentWidth += buttonWidth;
|
}
|
}
|
EditorGUILayout.EndHorizontal();
|
}
|
}
|