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);
}