- Home /
Mixed-state toggles for multi-object editing?
Component editors can support multi-object editing when the CanEditMultipleObjects
attribute is used.
The built-in editors can present 3 variations of the toggle control depending upon whether the they are checked, unchecked or contain a mixed state since one or more objects have differing values. The following screenshot illustrates each of the three states of the toggle control:
This tri-state toggle control is automatically provided by the default inspector implementation of custom components, and likewise can be presented using the EditorGUI.PropertyField
function.
For technical reasons I need to be able to render this control myself. How can I draw the "Mixed" variation of the toggle style within an editor GUI? Is this graphic part of the GUI.skin.toggle
style or another hidden internal style?
Answer by frarees · Mar 29, 2014 at 10:23 AM
Use EditorGUI.showMixedValue
.
E.g.
EditorGUI.showMixedValue = true;
bool value = EditorGUILayout.Toggle (...);
EditorGUI.showMixedValue = false;
SerializedProperty.hasMultipleDifferentValues
is very handy here to do what you need.
Also, I use to wrap properties in Begin/End calls (e.g. BeginColorGroup, BeginShowMixedValueGroup, etc... as Unity does with EditorGUI.BeginDisabledGroup
), this way is easier to keep track of that property state.