What is the mathematical formula for GUI dragging ?

Hi , when working with unity’s GUI , there is a formula which is used when creating a drag behavior for GUI.Box, GUI.Button, GUI.DrawTexture etc…

I want to be able to drag any GUI no matter where on the GUI i click and drag .
I just wrote this here but this code should do that .
The problem is , my drag formula is wrong .

for example

public class DragSomething: EditorWindow
{
    private Rect DrawTextureRect = new Rect(0, 0, 50, 50); // reck of gui we will drag
private bool Dragging ; // bool to check if we are dragging ...if mouse is down

private float Xpos, Ypos, Xwidth, Yheight; // so that we can work with each value 

private Rect GUIposition(Vector2 mousePosition,Rect guiRect)
{

    // our bad math formula which will drag our gui but actually just throws the gui into a corner ... literally
    Xpos = mousePosition.x - DrawTextureRect.x; // if we had 540 as mousePosition.x  and 500 as DrawTextureRect.x , Xpos would be equal to 40 ... which is very bad 

Ypos = mousePosition.y - DrawTextureRect.y;  // same here 

Xwidth = DrawTextureRect .width;// this is fine

Yheight = DrawTextureRect .height;// this is fine too

return(new Rect(Xpos, Ypos, Xwidth, Yheight));
}


void OnGUI()
{

 if (Event.current.type == EventType.MouseUp)
            {
                Dragging = false;
            }
 if (Event.current.type == EventType.MouseDown && DrawTextureRect.Contains (Event.current.mousePosition))
            {
                Dragging = true;
            }

if(Dragging )
{
GUIposition(Event.current.mousePosition,DrawTextureRect); // give it the mouse position and the rect of the bui we want to drag
}
}

GUI.DrawTexture(DrawTextureRect,sometexture);

}

When you capture mouse down in your Rect, find out how far you away from the Rects origin in x and y, then capture the mouse and subtract the x and y you found. Like what Glurth said.

Try this:

Make a Vector2 called cursorOffset.

Place this where you have Dragging = true, before it.

cursorOffset.x = Event.current.mousePosition.x - DrawTextureRect.x;
cursorOffset.y = Event.current.mousePosition.y - DrawTextureRect.y;

Then your math would be:

Xpos = Event.current.mousePosition.x - cursorOffset.x;
Ypos = Event.current.mousePosition.y - cursorOffset.y;

I think that might work.

I was given an example of offset usage which i had been unaware of

using UnityEngine;
using System.Collections;
using UnityEditor;

public class TestWindow : EditorWindow {

	[MenuItem ("Window/Test Window")]
	static void Init () {
		// Get existing open window or if none, make a new one:
		TestWindow window = (TestWindow)EditorWindow.GetWindow (typeof (TestWindow));
		window.Show();
	}
	
	Texture2D pixel;
	Rect windowRect;
	bool isDragging;
	Vector2 offset;
	
	public TestWindow()
	{
		pixel = new Texture2D(1, 3, TextureFormat.ARGB32, true);
		pixel.SetPixel(0, 0, new Color(1, 1, 1, 1));
		pixel.Apply();
		
		windowRect = new Rect(10,10,100,100);
		isDragging = false;
		offset = Vector2.zero;
	}
	
	void OnGUI()
	{
		//update
		Event evt = Event.current;
		
		switch(evt.type)
		{
			case EventType.MouseDown:
				if(windowRect.Contains(evt.mousePosition))
				{
					isDragging=true; 
                  
					//get offset from mouse
				offset = evt.mousePosition - windowRect.position;
				}
				break;
			case EventType.MouseUp:
				isDragging = false;
				break;
			case EventType.MouseDrag:
				if(isDragging)
				{
					windowRect.position = evt.mousePosition - offset;
				}
				break;
		}
		
		//render
		GUI.DrawTexture(windowRect,pixel);
		
		Repaint ();
	}