Search Unity

EditorGUILayout - get width of inspector window area?

Discussion in 'Immediate Mode GUI (IMGUI)' started by mas, Mar 16, 2011.

  1. mas

    mas

    Joined:
    Nov 14, 2010
    Posts:
    696
    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?
     
    UsefulYdyot likes this.
  2. Bryzi

    Bryzi

    Joined:
    Apr 27, 2011
    Posts:
    2
    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);
     
    Zephyr62, luizero00 and flashframe like this.
  3. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
    Screen.width returns relative to the current window.
     
  4. Bryzi

    Bryzi

    Joined:
    Apr 27, 2011
    Posts:
    2
    The docs for screen.width say...

    Code (csharp):
    1. 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.
     
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    When inside a OnInspectorGUI call, Screen.width and Screen.height returns the dimensions of the editor window / tab the inspector is located into.
     
  6. AlerZampi

    AlerZampi

    Joined:
    Dec 25, 2011
    Posts:
    27
    position.width?
     
  7. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    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).
     
  8. Innovative

    Innovative

    Joined:
    Jan 13, 2013
    Posts:
    4
    This worked for me but yes the scrollbar issue still remains.
    Code (csharp):
    1.  
    2.     public void OnGUI()
    3.     {
    4.         Event e = Event.current;
    5.         if (e.mousePosition.x > 0  e.mousePosition.y > 0  e.mousePosition.x < Screen.width  e.mousePosition.y < Screen.height)
    6.             Debug.Log(e.mousePosition);
    7.     }
     
  9. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  10. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797
    Awesome! Thank you for finding that :)
     
  11. Sycobob

    Sycobob

    Joined:
    Feb 1, 2013
    Posts:
    78
    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:

     
    codestage, PNUMIA-Rob and KaiClavier like this.
  12. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70
  13. Sycobob

    Sycobob

    Joined:
    Feb 1, 2013
    Posts:
    78
    Nope, it's just super poorly named. From the documentation:
     
  14. TomNCatz

    TomNCatz

    Joined:
    Jan 6, 2015
    Posts:
    24
  15. LostPenguin

    LostPenguin

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

    Code (CSharp):
    1. float contextWidth = (float)typeof(EditorGUIUtility).GetProperty("contextWidth", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
     
    Last edited: Mar 22, 2017
    ratking, Edy and MHDante like this.
  16. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    hm, might be a bug, but getting Screen.width while inside Editor.OnInspectorGUI() returns width of the inspector

    unity 2017.1

    dafuq
     
    Last edited: Sep 15, 2017
  17. Sycobob

    Sycobob

    Joined:
    Feb 1, 2013
    Posts:
    78
    As noted above in this thread:

    This is one of those poorly designed and documented APIs that plague the Editor side of things.
     
    IgorAherne likes this.
  18. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,023
    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

     
    Last edited: Feb 4, 2018
  19. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    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.
     
  20. ionic234

    ionic234

    Joined:
    Nov 7, 2017
    Posts:
    1
    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.
     
  21. BrinicleGames

    BrinicleGames

    Joined:
    Jan 7, 2019
    Posts:
    2
    EditorGUIUtility.currentViewWidth
     
    magique likes this.
  22. usernameHed

    usernameHed

    Joined:
    Apr 5, 2016
    Posts:
    93
    So, How can I get the height of the EditorWindow, WITH the scrollbar ?
     
  23. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    In Unity 2018, trying to get this to work inside Unity's own proprietary ProjectSettings window, I found:

    @Daniel_Brauer 's EditorGUIUtility.currentViewWidth: returns the width of the WINDOW not the width of the inspector PANEL (the window has a variable extra width, which is Unity's own column of selectable settings-names, and the user can re-size arbitrarily)
    @ionic234's EditorGUILayout.GetControlRect().width: returns a number that is just always broken and incorrect. It is sometimes wrong by +300 pixels. Then you move the window around using the mouse and suddenly it's wrong by 400 pixels. etc.

    ...fortunately, all this is going away in 2 years time once UIElements is in every Unity version, but without backports we're still supporting this (with no obvious way to fix Unity's missing API calls) :).
     
    mitaywalle and customphase like this.
  24. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    To get the current rendering EditorWindow, you can use the following:
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Reflection;
    4. using UnityEditor;
    5.  
    6. public static class Utility
    7. {
    8.    private static bool initializeCurrentEditorWindowMetadata;
    9.    private static PropertyInfo current;
    10.    private static FieldInfo m_ActualView;
    11.  
    12.    public static EditorWindow GetCurrentEditorWindow()
    13.    {
    14.        if (Utility.initializeCurrentEditorWindowMetadata == false)
    15.        {
    16.            Utility.initializeCurrentEditorWindowMetadata = true;
    17.            Utility.LazyInitializeCurrentEditorWindowMetadata();
    18.        }
    19.  
    20.        if (Utility.current == null)
    21.            return null;
    22.  
    23.        object guiView = Utility.current.GetValue(null, null);
    24.  
    25.        if (guiView != null)
    26.            return Utility.m_ActualView.GetValue(guiView) as EditorWindow;
    27.        return null;
    28.    }
    29.  
    30.    private static void LazyInitializeCurrentEditorWindowMetadata()
    31.    {
    32.        Type GUIViewType = typeof(Editor).Assembly.GetType("UnityEditor.GUIView");
    33.        if (GUIViewType != null)
    34.        {
    35.            Utility.current = GUIViewType.GetProperty("current", BindingFlags.Public | BindingFlags.Static);
    36.  
    37.            Type HostViewType = typeof(Editor).Assembly.GetType("UnityEditor.HostView");
    38.            if (HostViewType != null)
    39.                Utility.m_ActualView = HostViewType.GetField("m_ActualView", BindingFlags.NonPublic | BindingFlags.Instance);
    40.  
    41.            if (Utility.m_ActualView == null)
    42.                Utility.current = null;
    43.        }
    44.    }
    45. }
    46.  
    From that, you have access to its position.
     
  25. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Code (CSharp):
    1. private Rect _rect;
    2.         private float GetViewWidth()
    3.         {
    4.             GUILayout.Label("hack", GUILayout.MaxHeight(0));
    5.             if (Event.current.type == EventType.Repaint)
    6.             {
    7.                 // hack to get real view width
    8.                 _rect = GUILayoutUtility.GetLastRect();
    9.             }
    10.  
    11.             return _rect.width;
    12.         }
     
    TaHoSi, dyfer and Edy like this.
  26. Vowgan

    Vowgan

    Joined:
    Jan 12, 2017
    Posts:
    3
    For those wondering, screen.width gives you the correct number ASSUMING that your monitor is scaled to 100%. If it is not, for example you are using 150% scaling, it will give you the number 1.5x the width of your actual inspector.
     
  27. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Doesn't matter, the core APIs here are broken and will 'never' be fixed (according to Unity, in response to the bugs I filed on these APIs). UIToolkit team broke them (because the EditorGUI stuff is now rendered by UIToolkit, and UIToolkit's emulation-layer for EditorGUI, not directly by editor any more) and don't want to do anything to go back and fix them.