- Home /
How to correctly use EditorGUILayout.MaskField in multi-edit ?
In a custom inspector, I have a bitfield that I set with EditorGUILayout.MaskField, but I get a wierd behaviour when I'm multi-editing prefabs: the value of the last selected prefab overide the value of every prefabs, if I don't put the changeCheck, prefabs get messed as soon as I select them. Am I doing something wrong ?
[CustomEditor(typeof(Module))]
[CanEditMultipleObjects]
public class ModuleDrawer : Editor
{
SerializedProperty _serializedBitField ;
void OnEnable()
{
_serializedBitField = serializedObject.FindProperty("_bitField ");
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
EditorGUI.BeginChangeCheck();
_serializedBitField.intValue = EditorGUILayout.MaskField("BitField", _serializedBitField.intValue, randomStringArray);
if (EditorGUI.EndChangeCheck())
serializedObject.ApplyModifiedProperties();
}
Answer by Bunny83 · Jul 06, 2018 at 11:26 AM
Well, multi object editing is generally a bit tricky. To avoid unwanted changes you must prevent the assignment to _serializedBitField.intValue
. Whenever you assign a value here the serialized object / property will set the value for all objects represented by the SerializedObject.
One straight forward way would be:
int newVal = EditorGUILayout.MaskField("BitField", _serializedBitField.intValue, randomStringArray);
if (newVal !=_serializedBitField.intValue)
_serializedBitField.intValue = newVal;
This way you only actually change the value when you tinker with the maskfield UI. However for multiobject editing it's usually common to visually show the user that the property has multiple values.
Most editor controls which support multiediting internally check SerializedProperty.hasMultipleDifferentValues and replace the shown content by a dash. Of course that is not possible with the maskfield. You may just display an additional warning box if "hasMultipleDifferentValues" is true. Another solution is to simply omit the whole maskfield if you're editing multiple objects with different values and just show an info box instead.
Note that if you're editing multiple different values, the value that is returned when you read intValue is just the value of the first object. Unfortunately the SerializedProperty doesn't provide a way to read or write the individual values.
For example, have a look at Unity's implementation of the Popup field for SerializedProperties
Answer by mapbox · Jul 06, 2018 at 12:04 AM
Here's a way to convert the generated int from the EditorGUILayout.MaskField back to the array https://answers.unity.com/questions/319940/maskfield-selected-values.html
Thanks but this question was about fixing a weird behavior when multi-editing objects with $$anonymous$$askField controls, not "how to convert bitfield into array". by the way, since last year, I never found the answer and got used to manage around this.
Your answer
Follow this Question
Related Questions
Editor: How to display properties in inspector based on enum in same script ? 1 Answer
Serializable settings for custom PropertyDrawer 1 Answer
Custom Inspector Enum 1 Answer
Custom ordering of variables in inspector when using base and derived classes 1 Answer
Help with editor serialization 1 Answer