关于Unity自定义Inspector面板的一些自定义编辑器扩展

最近上官网学习了一些自定义编辑器扩展,可用于快速调试,下边是我写的一个案例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class LookAtPoint : MonoBehaviour
{
    
    
    public Vector3 lookAtPoint = Vector3.zero;

    // Update is called once per frame
   public void Update()
    {
    
    
        transform.LookAt(lookAtPoint);
    }
}

[CustomEditor(typeof(LookAtPoint))]
[CanEditMultipleObjects]
public class LookAtPointEditor:Editor
{
    
    
    SerializedProperty lookAtPoint;

    private void OnEnable()
    {
    
    
        lookAtPoint = serializedObject.FindProperty("lookAtPoint");
    }

    public override void OnInspectorGUI()
    {
    
    
        serializedObject.Update();
        EditorGUILayout.PropertyField(lookAtPoint);
   
        if (lookAtPoint.vector3Value.y > (target as LookAtPoint).transform.position.y)
        {
    
    
            EditorGUILayout.LabelField("(Above this object)");
        }
        if (lookAtPoint.vector3Value.y < (target as LookAtPoint).transform.position.y)
        {
    
    
            EditorGUILayout.LabelField("(Below this object)");
        }
        serializedObject.ApplyModifiedProperties();

    }

    public void OnSceneGUI()
    {
    
    
        var t = (target as LookAtPoint);

        EditorGUI.BeginChangeCheck();

        Vector3 pos = Handles.PositionHandle(t.lookAtPoint, Quaternion.identity);

        if (EditorGUI.EndChangeCheck())
        {
    
    
            Undo.RecordObject(target, "Move point");
            t.lookAtPoint = pos;
            t.Update();
        }

    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


class MyWindow : EditorWindow
{
    
    
    string myString = "Hello World";
    bool groupEnabled;
    bool myBool = true;
    float myFloat = 1.23f;


    [MenuItem("Window/My Window")]

    public static void ShowWindow()
    {
    
    
        EditorWindow.GetWindow(typeof(MyWindow));
    }

    void OnGUI()
    {
    
    
        GUILayout.Label("Base Settings", EditorStyles.boldLabel);
        myString = EditorGUILayout.TextField("Text Field", myString);

        groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
        myBool = EditorGUILayout.Toggle("Toggle", myBool);
        myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
       
    

        EditorGUILayout.EndToggleGroup();
    }
}

[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{
    
    
    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
    
    
        // Using BeginProperty / EndProperty on the parent property means that
        // __prefab__ override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var amountRect = new Rect(position.x, position.y, 30, position.height);
        var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}


[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
    
    
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
    
    
        // First get the attribute since it contains the range for the slider
        MyRangeAttribute range = (MyRangeAttribute)attribute;

        // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(position, property, range.min, range.max, label);
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label);
        else
            EditorGUI.LabelField(position, label.text, "Use MyRange with float or int.");
    }
}


猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/126020869