EditorGUILayout - get width of inspector window area?

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.

Anyone?

1 Like

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();

EditorGUIUtility.LookLikeInspector();
EditorGUIUtility.LookLikeControls(scale.width/2);

3 Likes

Screen.width returns relative to the current window.

5 Likes

The docs for screen.width say…

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.

When inside a OnInspectorGUI call, Screen.width and Screen.height returns the dimensions of the editor window / tab the inspector is located into.

4 Likes

position.width?

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).

This worked for me but yes the scrollbar issue still remains.

    public void OnGUI()
    {
        Event e = Event.current;
        if (e.mousePosition.x > 0  e.mousePosition.y > 0  e.mousePosition.x < Screen.width  e.mousePosition.y < Screen.height)
            Debug.Log(e.mousePosition);
    }

EditorGUIUtility.currentViewWidth is probably what you want. It doesn't include the scroll bar in the inspector.

22 Likes


Awesome! Thank you for finding that :-)

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:

[quote]
Hello,

We've checked the history of this code and as it turns out it never did account for the scrollbar.

We do have a function that accounts for the scrollbar, but at the moment it is internal.
We do plan on making it public, however there is no set date for that.

I think making a request for such a function in our feedback system (http://feedback.unity3d.com) could speed the things up, but that's all I could help with.
[/quote]

3 Likes

EditorGUIUtility.fieldWidth might be that new variable that accounts for the scrollbar? It appears to do what I expect it to.

Nope, it's just super poorly named. From the documentation:
[quote]
"The minimum width in pixels reserved for the fields of Editor GUI controls."
[/quote]

Since it was still an issue and i couldn't find a suggestion in their feedback box for it, I have added this as a suggestion here https://feedback.unity3d.com/suggestions/editorguilayout-get-available-width-of-inspector-window-area

I think the property is contextWidth, which has internal visibility. You can access it with reflection, but not really good practice:

float contextWidth = (float)typeof(EditorGUIUtility).GetProperty("contextWidth", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
3 Likes

hm, might be a bug, but getting Screen.width while inside Editor.OnInspectorGUI() returns width of the inspector

unity 2017.1

dafuq

As noted above in this thread:

This is one of those poorly designed and documented APIs that plague the Editor side of things.

1 Like

I have experimented with right width for an image in custom inspector in two cases: with scrollbar and without a scrollbar.
I made the result, which bring to you a flexible image size in any cases.

Check my Publisher Component: http://u3d.as/YZ3

https://www.youtube.com/watch?v=jFOt0B2AinY

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.

7 Likes