Im trying to pick up and drag GUI.Boxes around inside of a GUI.Window. Eventually ill be trying to grab them and move them from one window to another but ill just try to get this working for now :)
Here is what i have but it doesn't work and im not quite sure why..
My test print wont show up when i click on the button. When i move the test print up a line to check if my mouse is over the rect it does work. Where am i going wrong? Thanks for taking a look!
GUI.Button probably makes use of Event.Use(), so it eats the EventType.MouseDown event when activated. Move the GUI.button code to the bottom of the function so you can check stuff before the event is used.
Thanks for the tip, you right that worked great. I did run into a little problem unrelated to your help. Just thought i would share to let anyone else know that comes across this, Since the GUI.Windows set an independed coordinate system inside their dimentions, you need to account for this when trying to get button or box to stay under your cursor. I also updated the code a bit to run better. Im sure there is a better way to do this but i have it working great with this. Here you go!
var mdown = false; //var to turn on/off my mouse down effect. Couldnt figure out how to tell if it was STILL down, would always drop after i clicked
var t1 = Rect(20, 35, 60, 60); //standard rectangle variable to place inside of a gui object
function WindowBox (windowID : int) { //this is called to start a gui.window
windowBoxRect = Rect(Screen.width-240, Screen.height-465, 240, 395);//this reevaluates the window size/loc to changes the dimentions of the window to accomidate scaling gui and such i believe..
var fMouseX : float = ((Input.mousePosition.x)-(Screen.width-240)); //first half of equation, create a variable to track your mouse and 2nd half adjusts to the location of your GUI.Window because it has its own coordinate system inside of it.
var fMouseY : float = ((Screen.height - Input.mousePosition.y)-(Screen.height-465));//same as above
if (t1.Contains(Event.current.mousePosition))//checks to see if your mouse is inside your box/button you want to pick up and move
{
if (Event.current.type == EventType.MouseDown && mdown==false)//checks to see if you clicked and weather you currently have a "fake" mouse down check going on to symulate a constant mouse down not just an instant click, i found it drops the box right away with out doing this
{
mdown=true;
return;
}
if (Event.current.type == EventType.MouseDown && mdown==true)//releases your box from your mouse if you currently have it picked up
{
mdown=false;
}
}
if (mdown)//sets the location of the box to your cursor when the mouse is being clicked
{
t1 = Rect(fMouseX-25, fMouseY-25, 60, 60);//notice the -25, did this so the button would center on my cursor, otherwise it grabs on the top left corner. Based off of the size of your button of course
}
GUI.Button (Rect(t1), "Tool01");//your box being rendered in the GUI AFTER your mouse click events, thanks to Eric5h5
}
Thanks for the help with this! Hope this helps someone else.
public class PruebaWindow : MonoBehaviour { public Rect windowRect0 = new Rect(20, 20, 120, 50); public Rect windowRect5; void Start(){ windowRect5 = new Rect(100,20,80,100); }