Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

EditorGUILayout - get width of inspector window area?

Discussion in 'Extensions & OnGUI' 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?
     
  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);
     
    luizero00 and flashframe like this.
  3. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    463
    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:
    1,170
    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. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,225
    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,344
  10. flashframe

    flashframe

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

    Sycobob

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

     
    Lucid-Amusement and KaiClavier like this.
  12. KaiClavier

    KaiClavier

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

    Sycobob

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

    TomNCatz

    Joined:
    Jan 6, 2015
    Posts:
    10
  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
    MHDante likes this.
  16. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    309
    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:
    76
    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. atorisa

    atorisa

    Joined:
    Dec 1, 2013
    Posts:
    88
    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:
    286
    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.
     
    flashframe likes this.