- Home /
Can I change GUIButton behaviour?
Is there a way to change a GUIButton behaviour? I need it to be activated/pressed if I click outside the button, drag it in its area and then release like any other buttons you find out there.
The main reason for this is because we're using an ELO touchscreen monitor. Changing the mouse emulation mode to click on release works fine with GUIButtons, except that it disables any drag. And dragging stuff is something we need for this game.
Edit: I just realized that it behaves different when running withing Unity and compiled as a Windows standalone. Within Unity the touch with mouse emulation works wonderful, no problems at all.
Answer by Lex · Mar 11, 2010 at 01:42 PM
Yes I could.
The best way to doing so is to save the rectangles used for drawing the buttons on GUI, and using them on Update checking if mouse is colliding. Note that the mouse coordinates are inverse in Y axes compared with the GUI coordinates so here's the C# sample code:
private Rect myButtonRect;
void Start() { myButtonRect= new Rect(0, 0, 200, 100); }
void Update() { float fMouseX = Input.mousePosition.x; float fMouseY = Screen.height - Input.mousePosition.y; // Inverted Y
if (ButtonRect.Contains(new Vector2(fMouseX, fMouseY)))
{
// Then all you have to do is code your button behaviour for each mouse action
if (Input.GetMouseButton(0))
{
// Your code for mouse left click
}
if (Input.GetMouseUp(0))
{
// Your code for left button released
}
// And so on...
}
}
void OnGUI() { GUI.Button(myButtonRect, "Whatever"); }