- Home /
Editing how bools appear in the Inspector
If I have a series of booleans, in this case 9, is there a way to format them inside of the inspector for quality of life purposes?
For example:
Turning ^this, into this -
These are just examples, of course. It's a relatively small thing, but when setting up a system for people of various skill/experience levels to be working with, having the inspector set up like this would certainly make processes much easier, as it's significantly more intuitive.
Is this possible? I've never really considered modifying the inspector before.
Answer by Namey5 · Feb 23, 2021 at 06:18 AM
You could do this fairly easily with a custom editor, however if this was something that occurs in numerous scripts I would suggest putting all spaces into a single object and creating a custom PropertyDrawer, something along the lines of;
[System.Serializable]
public struct Spaces
{
public bool space1;
public bool space2;
public bool space3;
public bool space4;
public bool space5;
public bool space6;
public bool space7;
public bool space8;
public bool space9;
}
...
using UnityEditor;
[CustomPropertyDrawer (typeof (Spaces))]
public class SpacesDrawer : PropertyDrawer
{
public override void OnGUI (Rect a_Rect, SerializedProperty a_Property, GUIContent a_Label)
{
//Shorthand to make writing easier
float lh = EditorGUIUtility.singleLineHeight;
//Draw our label in the full rect (will self centre) but leave room at the end for the bools
Rect labelRect = new Rect (a_Rect.x, a_Rect.y, a_Rect.width - lh * 3f, a_Rect.height);
EditorGUI.LabelField (labelRect, a_Label);
//This is where the bools will start on the x-axis
float boolStartX = a_Rect.x + labelRect.width;
//Initialise the bool rect to the top-left corner (1 line wide and tall)
Rect boolRect = new Rect (boolStartX, a_Rect.y, lh, lh);
//We want the bools to go left -> right then top -> bottom, so start with the y-axis as the outside loop
for (int y = 0; y < 3; y++)
{
//Re-initialise the x-axis with every new row
boolRect.x = boolStartX;
for (int x = 0; x < 3; x++)
{
//Find the current index (starting at 1)
int currentBool = x + y * 3 + 1;
//Draw the bool as normal (without a label)
EditorGUI.PropertyField (boolRect, a_Property.FindPropertyRelative ("space" + currentBool), GUIContent.none);
//Move to the next bool in the row
boolRect.x += boolRect.height;
}
//Move to the next row of bools
boolRect.y += boolRect.width;
}
}
//We need to override the property height - let's make it 3 lines (a regular property will be 1 line high)
public override float GetPropertyHeight (SerializedProperty a_Property, GUIContent a_Label)
{
return EditorGUIUtility.singleLineHeight * 3f;
}
}