CustomEditor or PropertyDrawers? I can't decide

So for the last couple weeks, I have been tampering with making my own custom inspector to make it easier for me to add stuff to the game. I got to the point where I made foldouts, showing/hiding values based on bool values, titles, etc. But then I wasn’t too happy that I couldn’t seem to find a way to have multiple variables on the same line using EditorGUILayout. So i went hunting as I’ve seen it done before. I came across Property Drawers and still, they confuse me a little bit. Does a property drawer only work for one set of properties? I.e. on one line, I might have a “bool, bool, string” and then on the next line underneath, “int, float, bool” does this mean I have to make 2 property drawers to display two different sets of properties on different lines? If someone could explain, in simple words…I’m still not a pro just yet xD what’s better? CustomEditor using EditorGUILayout or using PropertyDrawers?

P.S. If someone could PLEEEEEEASE tell me if it is possible to change the text colour of a property field, e.g. where it says “Something (GameObject)” ? I was trying to do it for ages and still came up with no answers so I’m not sure if its possible.

Thanks :smile:

You can have several things on a line with EditorGUILayout:

EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Some int");
myInt = EditorGUILayout.IntField(myInt);
EditorGUILayout.EndHorizontal;

You can also nest horizontals and verticals inside of each other.

Property drawers are used to specify how specific elements are laid out, rather than laying out an entire script. I use property drawers mainly for two things:

  • Drawing elements of a specific type in a certain way
    So if you have a custom class in a public field, you can set up how it’s drawn with a property drawer

  • Drawing with custom attributes
    You can define [YourOwnAttribute] to be drawn in a specific way. This is how the [Header] attribute works - it makes a variable be twice as big, and puts a header text above the variable.

You change the color of elements by setting the GUI.Color, GUI.BackgroundColor and GUI.contentColor.

1 Like

Thanks :slight_smile: I’ll give the vertical and horizontal a try. I’m surprised I didn’t try that in the first place. Will post again when I’ve tried it all out :smile:

Okay, so I tried it. The problem I am facing now is that the spacing between labels and property values are so far wide, I can’t keep multiple values on the same line.

Basically I want it to be like this on one line…

Label (Audio Clip) Label (bool) Label (int)

I tried using indent level, but when the inspector is shrunk/expanded, it gets all muddled up. :frowning:

You can specify the width of a label with the GUILayout.Width call like this:

EditorGUILayout.Label("Some text", GUILayout.Width(100)); //creates a label that's no more than 100 pixels wide

Elements in a Horizontal layout floats to the left, so if you restrict their width, they’ll not have any extra spacing.

The Width method (and a bunch of the other GUILayout methods) returns a GUILayoutOption object, and most of the layout methods takes an arbitrary amount of those as their final parameter, so you can adjust the layout of stuff with those.