Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Mouse position inside GUI Button?

Discussion in 'Immediate Mode GUI (IMGUI)' started by monark, Jul 14, 2008.

  1. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I'd like to find the mouse position relative to a GUI button.
    I can get the screen position of the mouse using "Input.mousePosition" and I could work out the relative position myself. But I see there is a "GetCursorPixelPosition" which *might* do what I want, but I can't see how to use it.
    Is this the way to do this? Any pointers? The down side to working it out myself is I need to use a static fixed layout in order to calculate the offset.

    cheers
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Use Event.current.mousePosition - this will always be in the same coordinate system as you use, correctly tracking scrollviews, begingroup, GUI.Matrix, whatnot

    Code (csharp):
    1.  
    2. function OnGUI () {
    3.    var rect = Rect (10,10,20,100);
    4.    if (Rect.Contains (Event.current.mousePosition)
    5.       print ("Mouse is inside the rect");
    6. }
    7.  
     
  3. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    When I print out the actual coordinates returned by that function they still seem to be in screen space, so i still need to adjust them for the position of the box.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you have the rect with the position (that you somewhere must define), so what is the problem to substract the first 2 entries of that rect from the mouse coords to get relative box coords? *don't see any at all, not even with dynamic positioning as the rect definition is there anyway no mather if static positioned or dynamic*
     
  5. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Sorry I'm still new to this, when you dynamically let unity do the layout how do you get to the rect definition? In the manual examples they leave out the rect part if you want automatic layout.
     
  6. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Now you're entering the weird and wonderful world of mixing GUI GUILayout.

    Basically what you want is to use GUILayoutUtility to get allocate and get a rect from the layouting system, then feed that into both your check and a GUI.button.

    Code (csharp):
    1.  
    2. var r = GUILayoutUtility.GetRect (100,20); // 100,20 is the size.
    3. GUI.Button (r, "Hello");
    4. if (r.Contains (Event.current.mousePosition))
    5.    print "Inside button";
    6.  
    If you want to match the builtin behavior completely, you'll want another variant of GetRect:
    Code (csharp):
    1.  
    2. // do some initialization for GetRect.
    3. var content = GUIContent ("Button Text");
    4. var style = GUIStyle ("button");
    5.  
    6.  // Gets a rect with the size needed for  "Button Text" when rendered with the "button" style.
    7. var r = GUILayoutUtility.GetRect (content, style);
    8. GUI.Button (r, content, style);
    9. if (r.Contains (Event.current.mousePosition))
    10.    print "Inside button";
    11.  
     
  7. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Cheers, very useful stuff. No way I would have got that from the help...
     
  8. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I found the easiest way to do this now. Just put it inside a group block, that way the coordinates are returned relative to the group Rect. Easy.
     
  9. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    This sounds very interesting. What do you mean by a group block though?
     
  10. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    in an OnGUI function you can define groups

    GUI.BeginGroup( Rect(100, 100, 300, 300) );
    .
    .
    .
    GUI.EndGroup();


    Anything inside the group block uses coordinates relative to that group. If you want to move all the items defined inside the group just change the group rect function parameters.

    Look in the manual for the OnGUI help its all in there.

    You can nest groups so what I did was put a second group just around my button that I want the coordinates for then my Event.current.mousePosition call gives values relative to just that button where ever it is on screen.
     
  11. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    Brilliant! Thanks a ton - works like a charm!