Unity CustomEditor 自定显示Inspector面板属性

赫连靖琪
2023-12-01

1.Unity中,我们想自定义组件在Inspector面板中的属性显示。这时候就需要用到CustomEditor属性了。

2.实现案例:

[CustomEditor(typeof(CircleImage), true)]
[CanEditMultipleObjects]
public class CircleImageEditor : ImageEditor
{
    private SerializedProperty _segements; //序列化三角形个数属性
    private SerializedProperty _showPercent; //序列化显示百分比
    private SerializedProperty _accurateRaycast; //序列化射线精准检测开关

    protected override void OnEnable()
    {
        base.OnEnable();
        _segements = serializedObject.FindProperty("segments");
        _showPercent = serializedObject.FindProperty("showPercent");
        _accurateRaycast = serializedObject.FindProperty("accurateRaycast");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        serializedObject.Update();

        EditorGUILayout.Space(); //显示增加一行空格显示
        EditorGUILayout.PropertyField(_segements); //创建一个字段
        EditorGUILayout.Slider(_showPercent, 0, 1, new GUIContent("showPercent")); //创建一个滑动条
        EditorGUILayout.PropertyField(_accurateRaycast);
        //下面固定
        serializedObject.ApplyModifiedProperties();
        if (GUI.changed)
            EditorUtility.SetDirty(target);
    }
}
 类似资料: