[Unity] Inspector自定义

using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TButton))]
internal sealed class TButtonInspector :Editor
{
    SerializedProperty OnClick = null;
    SerializedProperty Interactable = null;
    public enum Transition
    {
        None,
        ColorTint,
        SpriteSwap,
        Animation
    }
    private GameObject graphic;
    private Transition transition = Transition.ColorTint;
    public override void OnInspectorGUI()
    {
        EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
        {
            EditorGUILayout.PropertyField(Interactable);
            EditorGUILayout.EnumPopup("Transition", transition);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical(GUILayout.Width(6));
            EditorGUILayout.Space();
            EditorGUILayout.EndVertical();
            EditorGUILayout.BeginVertical();
            EditorGUILayout.ObjectField("Target graphic", graphic, typeof(GameObject), false);
            if (graphic == null)
                EditorGUILayout.HelpBox("You must have Target graphic", MessageType.Warning);
            EditorGUILayout.ColorField("Normal Color", Color.white);
            EditorGUILayout.ColorField("Highlighted Color", Color.white);
            EditorGUILayout.ColorField("Pressed Color", Color.gray);
            EditorGUILayout.ColorField("Disabled Color", Color.gray);
            EditorGUILayout.Slider("Color Multiplier", 1, 1, 10);
            EditorGUILayout.FloatField("Fade Duration", 0.1f);
            EditorGUILayout.EnumPopup("Navigation", transition);

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical(GUILayout.Width(180));
            EditorGUILayout.Space();
            EditorGUILayout.EndVertical();
            EditorGUILayout.BeginVertical();
            if (GUILayout.Button("Visualize"))
            {
                Debug.Log("检测到点击了");
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(OnClick);
        }
        EditorGUI.EndDisabledGroup();
        serializedObject.ApplyModifiedProperties();
    }

    private void OnEnable()
    {
        Interactable = serializedObject.FindProperty("Interactable");
        OnClick = serializedObject.FindProperty("onClick");
    }
}
using System;
using UnityEngine;
using UnityEngine.Events;

[DisallowMultipleComponent]
public class TButton : MonoBehaviour
{
    [SerializeField]
    private bool Interactable = true;
    [SerializeField]
    private OnClick onClick;
}

[Serializable]
public class OnClick : UnityEvent { }

你可能感兴趣的:(开发心得,unity,游戏引擎)