0
\$\begingroup\$

I has a PropertyDrawer, everything goes fine, except one feature: I can't figure out how to add button and I use checkbox instead

Screenshot

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer (typeof (InteractiveObject))]
class InteractiveObjectPropertyDrawer : PropertyDrawer {

public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {

    EditorGUI.BeginProperty (position, label, property);
    position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
    var indent = EditorGUI.indentLevel;
    EditorGUI.indentLevel = 0;
    Rect colorRect = new Rect (position.x, position.y, 40, position.height);
    Rect unitRect = new Rect (position.x + 50, position.y, 90, position.height);
    Rect nameRect = new Rect (position.x + 150, position.y, position.width - 150, position.height);

    EditorGUI.BeginChangeCheck();
    EditorGUI.PropertyField (colorRect, property.FindPropertyRelative ("show"), GUIContent.none);
    if (EditorGUI.EndChangeCheck() ) {
        RenderSettings.skybox.SetColor("_highlight", property.FindPropertyRelative ("id").colorValue);
    }

    EditorGUI.PropertyField (unitRect, property.FindPropertyRelative ("name"), GUIContent.none);
    EditorGUI.PropertyField (nameRect, property.FindPropertyRelative ("name"), GUIContent.none);
    EditorGUI.indentLevel = indent;
    EditorGUI.EndProperty ();
}
}

How I can add button there?

\$\endgroup\$
  • \$\begingroup\$ Presumably you tried GUILayout.Button as recommended here? What went wrong? \$\endgroup\$ – DMGregory Feb 14 '19 at 19:34
  • \$\begingroup\$ @DMGregory yes, in this case buttons appear under all listed items... \$\endgroup\$ – Stranger in the Q Feb 14 '19 at 20:23
  • 1
    \$\begingroup\$ GUILayout is not supported on PropertyDrawers. You need to just use the GUI class. \$\endgroup\$ – Ed Marty Feb 15 '19 at 0:30
2
\$\begingroup\$

I've found how to add button to PropertyDrawer here

code:

 if (GUI.Button(text, "show")) {
     ...
 }

and result:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.