TextmeshProExtensionPackage/Packages/com.bywaystudios.textmeshpro.ext/Editor/TextMeshProBywayUIInspector.cs
2026-01-11 21:44:32 +08:00

161 lines
6.8 KiB
C#

using TMPro;
using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Byway.TMP
{
[CustomEditor(typeof(TextMeshProBywayUI))]
public class TextMeshProBywayUIInspector : TMP_EditorPanelUI
{
static readonly GUIContent k_HasOutlineLabel = new GUIContent("Outline", "Should the text Contains the outline.");
static readonly GUIContent k_OutlineColorLabel = new GUIContent("Outline Color", "The outline color of the text.");
static readonly GUIContent k_OutlineWidthLabel = new GUIContent("Outline Width", "The outline width of the text.");
static readonly GUIContent k_HasUnderlayLabel = new GUIContent("Underlay", "Should the text Contains the underlay.");
static readonly GUIContent k_UnderlayColorLabel = new GUIContent("Underlay Color", "The underlay color of the text.");
static readonly GUIContent k_UnderlayOffsetXLabel = new GUIContent("Underlay Offset X", "The underlay offset X of the text.");
static readonly GUIContent k_UnderlayOffsetYLabel = new GUIContent("Underlay Offset Y", "The underlay offset Y of the text.");
protected SerializedProperty m_FontHasOutlineProp;
protected SerializedProperty m_FontOutlineColorProp;
protected SerializedProperty m_FontOutlineWidthProp;
protected SerializedProperty m_FontHasUnderlayProp;
protected SerializedProperty m_FontUnderlayColorProp;
protected SerializedProperty m_FontUnderlayOffsetXProp;
protected SerializedProperty m_FontUnderlayOffsetYProp;
protected override void OnEnable()
{
m_FontHasOutlineProp = serializedObject.FindProperty("m_OverrideHasOutline");
m_FontOutlineColorProp = serializedObject.FindProperty("m_OverrideOutlineColor");
m_FontOutlineWidthProp = serializedObject.FindProperty("m_OverrideOutlineWidth");
m_FontHasUnderlayProp = serializedObject.FindProperty("m_OverrideHasUnderlay");
m_FontUnderlayColorProp = serializedObject.FindProperty("m_OverrideUnderlayColor");
m_FontUnderlayOffsetXProp = serializedObject.FindProperty("m_OverrideUnderlayOffsetX");
m_FontUnderlayOffsetYProp = serializedObject.FindProperty("m_OverrideUnderlayOffsetY");
base.OnEnable();
}
protected override void DrawExtraSettings()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_FontHasOutlineProp, k_HasOutlineLabel);
if (m_FontHasOutlineProp.boolValue)
{
EditorGUI.indentLevel += 1;
EditorGUILayout.PropertyField(m_FontOutlineColorProp, k_OutlineColorLabel);
EditorGUILayout.PropertyField(m_FontOutlineWidthProp, k_OutlineWidthLabel);
EditorGUI.indentLevel -= 1;
}
EditorGUILayout.PropertyField(m_FontHasUnderlayProp, k_HasUnderlayLabel);
if (m_FontHasUnderlayProp.boolValue)
{
EditorGUI.indentLevel += 1;
EditorGUILayout.PropertyField(m_FontUnderlayColorProp, k_UnderlayColorLabel);
EditorGUILayout.PropertyField(m_FontUnderlayOffsetXProp, k_UnderlayOffsetXLabel);
EditorGUILayout.PropertyField(m_FontUnderlayOffsetYProp, k_UnderlayOffsetYLabel);
EditorGUI.indentLevel -= 1;
}
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
(target as TextMeshProBywayUI).NotifyHelper();
}
base.DrawExtraSettings();
}
[MenuItem("GameObject/UI/Text - Textmesh Pro - Byway", false, 30)]
private static void CreateTextmeshProByway(MenuCommand command)
{
// Check if there is a Canvas in the scene
Canvas canvas = Object.FindObjectOfType<Canvas>();
if (canvas == null)
{
// Create new Canvas since none exists in the scene.
GameObject canvasObject = new GameObject("Canvas");
canvas = canvasObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
// Add a Graphic Raycaster Component as well
canvas.gameObject.AddComponent<GraphicRaycaster>();
Undo.RegisterCreatedObjectUndo(canvasObject, "Create " + canvasObject.name);
}
GameObject go = new GameObject("Text(Byway)");
RectTransform goRectTransform = go.AddComponent<RectTransform>();
Undo.RegisterCreatedObjectUndo((Object)go, "Create " + go.name);
// Check if object is being create with left or right click
GameObject contextObject = command.context as GameObject;
if (contextObject == null)
{
GameObjectUtility.SetParentAndAlign(go, canvas.gameObject);
TextMeshProUGUI textMeshPro = go.AddComponent<TextMeshProUGUI>();
go.AddComponent<TextMeshProBywayUIRuntimeHelper>();
textMeshPro.text = "New Text";
textMeshPro.alignment = TextAlignmentOptions.TopLeft;
}
else
{
if (contextObject.GetComponent<Button>() != null)
{
goRectTransform.sizeDelta = Vector2.zero;
goRectTransform.anchorMin = Vector2.zero;
goRectTransform.anchorMax = Vector2.one;
GameObjectUtility.SetParentAndAlign(go, contextObject);
TextMeshProBywayUI textMeshPro = go.AddComponent<TextMeshProBywayUI>();
go.AddComponent<TextMeshProBywayUIRuntimeHelper>();
textMeshPro.text = "Button";
textMeshPro.fontSize = 24;
textMeshPro.alignment = TextAlignmentOptions.Center;
}
else
{
GameObjectUtility.SetParentAndAlign(go, contextObject);
TextMeshProBywayUI textMeshPro = go.AddComponent<TextMeshProBywayUI>();
textMeshPro.text = "Byway";
textMeshPro.alignment = TextAlignmentOptions.TopLeft;
}
}
// Check if an event system already exists in the scene
if (!Object.FindObjectOfType<EventSystem>())
{
GameObject eventObject = new GameObject("EventSystem", typeof(EventSystem));
eventObject.AddComponent<StandaloneInputModule>();
#if UNITY_5_3_OR_NEWER
// Nothing
#else
eventObject.AddComponent<TouchInputModule>();
#endif
Undo.RegisterCreatedObjectUndo(eventObject, "Create " + eventObject.name);
}
Selection.activeGameObject = go;
}
}
}