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.

[Solved]Relative Mouse pos inside a Rect?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Cyril, Apr 27, 2010.

  1. Cyril

    Cyril

    Joined:
    Mar 19, 2010
    Posts:
    24
    Hello everyone,

    im trying to Drag a GUI.Box and having problems with the
    x, y position of the box.
    how would i get the relative mouse position inside the dragable rect instead of screen position?
    i want to avoid that the GUI.Box jumps to the Mouse Cursor Position.






    Code (csharp):
    1.  
    2. public var x : int; //300 GUI.Box Pos.x
    3. public var y : int; //200 GUI.Box Pos.y
    4. public var w : int = 333; // GUI.Box width
    5. public var h : int = 242; // GUI.Box height
    6.  
    7. var rect = Rect( x, y,  w, h ); //GUI.Box rect
    8. var rectItems = Rect( rect.x, rect.y,  300, 20 ); // Dragable area
    9.  
    10.             if(rectItems.Contains(Event.current.mousePosition)  Input.GetMouseButton(0)  invScript.MouseTex == null)
    11.             {
    12.                 drag();
    13.             }
    14.     else    if(Input.GetMouseButtonUp(0))
    15.             {
    16.                 drop();
    17.             }
    18.  
    19. if (dragging == true)
    20.             {
    21.                 var initPt = Input.mousePosition;
    22.                 x = initPt.x - (rectItems.width * .5); // Problem here (rectItems.width * .5);
    23.                 y = (Screen.height - initPt.y )  - (rectItems.height * .5); // Problem here (rectItems.height * .5);       
    24.             }
    25.            
    26.  
    27.             GUI.Box(rect, "Inventory", GUIStyle ("box") );
    Thanks fo any help/hints in advance!.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to subtract the XY position of the draggable area's rectangle from the mouse position to get the position relative to the area:-
    Code (csharp):
    1. var mPos = Input.mousePosition;
    2.  
    3. var relX = mPos.x - rect.x;
    4. var relY = mPos.y - rect.y;
     
    ROBYER1 likes this.
  3. Cyril

    Cyril

    Joined:
    Mar 19, 2010
    Posts:
    24
    Thanks for the hint andeeee!
    had to script alot more than 3 lines :p
    maybe its not professional way...
    it works perfect in Unity Editor but not in standalone and webplayer guessing it has to do with the screen.height...
    gona try to figure out tommorrow!

    Thanks so far!.

    EDIT :: Solved!
     
  4. Tushar_kmphasis

    Tushar_kmphasis

    Joined:
    Oct 5, 2019
    Posts:
    7
    Hello andreeee My Rect area is dragable zoomable so if we want the mouse position over RectView then what to do??
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,103
    This thread is almost ten years old. If you check the profile of the member you're nagging, you'd see that they were last on the forums in 2015.