Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Getting Current Rect's width

Discussion in 'Immediate Mode GUI (IMGUI)' started by The_BenEvans, Jun 11, 2019.

  1. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    137
    Hi,

    I have a 5 x 3 grid of buttons that I want to display in both the inspector and a Editor Window.

    My current thought process is that I need to get the width of the available layout space before drawing, and then divide by 5 to get the size of each button container. How should I get the available layout width? (Screenshot 1)

    I'm also drawing this within an Odin Foldout elsewhere, so it needs to take account of the current Rect I believe, instead of just the inspector as a whole. (Screenshot 2)

    Thanks in advance

    upload_2019-6-11_12-13-12.png
    Screenshot 1

    upload_2019-6-11_12-16-46.png
    Screenshot 2
     
  2. IC_

    IC_

    Joined:
    Jan 27, 2016
    Posts:
    48
  3. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    137
    Thanks for your reply, I did try Screen.width, but it was getting the width of the entire custom window (I should have mentioned before that it's 1 of 4 different width-sized columns within a custom window). But it did prompt me to try again.

    Anyway, I've done a dirty hack job to get it to work by adding a GULayout.Label() at the start of the OnInspectorGUI, which expands itself to fit the width correctly, giving me something solid to use GUILayoutUtility.GetLastRect() with (and doesn't throw a bunch of errors when inspecting the SO)!

    Hurray for hacks!

    upload_2019-6-25_17-34-24.png
     
  4. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    421
    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.         }
     
    chadfranklin47 likes this.
  5. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    204
    Thanks a lot, this was bugging me for a while. Though when attempting to center an EditorGUI.DrawPreviewTexture(), I needed to do:

    Code (CSharp):
    1. private float GetViewWidth()
    2. {
    3.     GUILayout.Label("hack", GUILayout.MaxHeight(0));
    4.     if (Event.current.type == EventType.Repaint)
    5.     {
    6.         // hack to get real view width
    7.         _rect = GUILayoutUtility.GetLastRect();
    8.     }
    9.  
    10.     return _rect.position.x * 2f + _rect.width;
    11. }
    Though I should probably rename the method.

    Then to draw the preview:
    Code (CSharp):
    1. EditorGUI.DrawPreviewTexture(new Rect((viewWidth / 2) - (desiredTextureWidth / 2), verticalRect.position.y, desiredTextureWidth, desiredTextureHeight), previewTexture);
     
    Last edited: Feb 25, 2022