CustomPropertyDrawer can't restrict multi editing

Hi There,

I want to disable multi object editing for any component which has a variable using my CustomPropertyDrawer. However I don’t want to write a new custom editor for every single component that declares this variable type. Is this at all possible?

Alternatively, Unity seems to detect fields which differ in value by putting a hyphen in, ‘-’. Is there a way I can enable this functionality on my CustomPropertyDrawer?

Thanks in advance.

Solution 1

Allow multi-edit on the custom property drawer (supports hyphen)

Custom property drawers support multi-editing as long as they only draw properties. If you are drawing with a line such as:

floatValue = EditorGUI.FloatField (position, floatValue);

then multi-edit won’t be supported. Instead, the value will belong to the component of the last object selected ( property.serializedObject.targetObject). It may be convenient in some situations, but also tricky what feels like a multi-edit is actually a single edit.

Instead, use:

		EditorGUI.PropertyField (position, property.FindPropertyRelative ("floatValue"), new GUIContent ("My Float"));

There is exactly the same issue with custom inspectors if you allow multi-editing but override OnInspectorGUI and use target. Instead, you should use serializedObject.FindProperty().

Result below (Alert Box has a custom drawer):

79845-unity-editor-custom-drawer-multi-edit-supported.png

Solution 2

During multi-editing, hide the custom PropertyDrawer and only that. Keep showing the other properties of the component.

In your custom PropertyDrawer class, check if property.serializedObject.isEditingMultipleObjects is true. If so, return immediately so that nothing is drawn. This is possible because the serialized object contains information on both the property and the inspected components (stored in property.serializedObject.targetObjects).

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    if (property.serializedObject.isEditingMultipleObjects)
        return;

    // usual drawing code here
    // ...
}

Don’t forget to shrink the height used by your drawer to 0 in multi-edit:

public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
    if (property.serializedObject.isEditingMultipleObjects)
        return 0f;
        
    // usual height calculation here
    // ...
}

Result below:

79844-unity-editor-custom-drawer-multi-edit-removed.png

References:

Editor.target API

Custom Editor : Call a function on multiple selected objects