I was wondering if it is possible to retrieve the width of the inspector window somehow. I am trying to create an custom inspector and I want to set the label and field widtn of an popup relative to the width of the window.
so 50%, 50%
with EditorGUIUtility.LookLikeControls I can only set the widths to a static value , not procentual relative to the entire width.
When I would have the Inspector Window’s width, I could calculate those 50% width values myself.
I found this post via a google search wanting to do the same thing. In the end I did this (which worked) although there must be a nicer way to do this, it wasnt that obvious in the docs so i ended up doing this. Essentially filling up the space and then checking what the width was.
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
Rect scale = GUILayoutUtility.GetLastRect();
This is the actual width of the player window (in fullscreen it is also the current resolution)
It was the width of the inspector window we were looking for. It would be nice to have a simple var to read :). I haven’t tried it from an inspector window yet but will double check later.
Was looking for the same info, and can confirm that Screen.width doesn’t return the correct size. Mainly, because it returns the panel size WITHOUT scrollbars, and there is no way to know if a panel has scrollbars or not (when being scrolled directly by Unity, and not by a ScrollView).
In case anyone else comes across this post, this does not appear to be accurate. currentViewWidth does not change based on the presence of the scroll bar. I reported this issue and received the following repsonse:
I have experimented with right width for an image in custom inspector in two cases: with a scrollbar and without a scrollbar.
I made the result, which bring to you a flexible image size in any cases.
If you want the size of a window you can just use the position property, it will return a Rect with (x=where it starts in x, y= where it starts in y, width, height)
I would also like to take the chance, since i wouldn’t be the first, to promote my asset. Voltage Editor UI
here is an example of what you can do, and it works for Custom Editors as well.
The most accurate way i’ve found of doing this is
rect = EditorGUILayout.GetControlRect(true, 0f). This actually reserves a rectangle in the editor for drawing EditorGUI controls so the 0f is to give that reserved space no height. This rect is effected by the scrollbars but also includes the padding.
Alternativly you can give it the desired height and build your EditorGUI controls using rect positioning however getting the default widths of all the content is a little obtuse. The main label’s dynamic width can be found using
EditorGUIUtility.labelWidth but i have yet to be able to find where the dynamic width for fields can be found.