I am writing a property drawer which draws Vector4s as single lines in arrays using EditorGUI.Vector4Field().
I would like to keep the functionality of having it expand to two lines when the inspector is narrow.
something along these lines works
EditorGUIUtility.currentViewWidth < 345 ? baseHeight * 2 : baseHeight;
but how do I get the correct value instead of the magic number 345?
You will have to make a
TL;DR EditorGUIUtility.wideMode returns true when vector elements take up one line, and false when they take up 2. Although it doesn’t directly give the value at which it changes, for this use case, it seems to be a perfect fit.
I think I finally found a solution.
I was having the exact same problem, except using a constant value wasn’t an option because I used the UI Toolkit, which (in my case at least) changed the viewport width at which vector fields break to a new line. However, once I found this post using EditorGUIUtility.currentViewWidth, I took a look at the EditorGUIUtility class. Low and behold, there is a property called wideMode that returns true when vector labels and input boxes are on one line, and false when they are on separate lines. Although this doesn’t give us the exact point at which it switches over, it works perfectly for my (and from what I can tell, your) use case. Plus, it seems to work more consistently because it (seemingly) relies on the width of the containing Rect instead of the containing viewport, so if your Rect takes up less than the whole viewport, it will still work correctly, unlike with a hard-coded value.
Your prior example of:
return EditorGUIUtility.currentViewWidth < 345 ? baseHeight * 2 : baseHeight;
would instead become:
return !EditorGUIUtility.wideMode ? baseHeight * 2 : baseHeight;
Thank you so much for this post, I probably wouldn’t have even remembered EditorGUIUtility, let alone thought to check there, without this. Additionally, that default value may come in handy down the line.