Search Unity

Finding position of GUILayout rect

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ryuuguu, Nov 25, 2008.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    In my Tutorial screens I draw line from a piece of text in a GUILayout.Label to an object on screen.So I want to find the bounding GUI rect for a GUILayout control. Does anyone have a way to get the bounding rectangle from GUILayout control?

    The best solution I have come up with is to write my own custom GUILayout controls that return the bounding layout rectangle. like the following .(The code below is copied from one of Nicholas's posts)
    Code (csharp):
    1. // do some initialization for GetRect.
    2. var content = GUIContent ("Button Text");
    3. var style = GUIStyle ("button");
    4.  
    5.  // Gets a rect with the size needed for  "Button Text" when rendered with the "button" style.
    6. var r = GUILayoutUtility.GetRect (content, style);
    7. GUI.Button (r, content, style);
    8. return r
    Cheers,
    Grant
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Code (csharp):
    1. // do some initialization for GetRect.
    2. var content = GUIContent ("Button Text");
    3. var style = GUI.skin.GetStyle( "Button" );
    4.  
    5.  // Gets a rect with the size needed for  "Button Text" when rendered with the "button" style.
    6. var r = GUILayoutUtility.GetRect (content, style);
    7. GUI.Button (r, content);
    8. return r
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    There is also GUILayoutUtility.GetLastRect(). But it seems its not documented.
     
  4. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    thanks that exactly the tiype of thing I need, I'll try it out.
     
  5. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    thanks that exactly the tiype of thing I need, I'll try it out.
     
  6. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    if you use GUILayoutUtility.GetLastRect() you should put the related code in a if block checking for Event.current.type==EventType.repaint , because OnGUI is called many times and during repaint events it will have the correct rect other time the layout may not be ready yet so it returns the wrong rect.
     
    OSG-Bart likes this.