- Home /
GUI Button OnMouseUp?
Hello, how can I make something happen when I release my mouse from my button?
This is my code in C# for the MouseDown:
if(GUI.Button(new Rect(0, ((Screen.height / 2) - 128), 256, 256), "", GUILeft)) {
TurnLeftInputDown();
}
This is how it should act: When you press the button it should run the "TurnleftInputDown" function 1 time, that's why I don't use RepeatButton.
And when you release the mouse, it should run another function.
Thanks, Andreas :)
Actually GUI buttons execute on mouse up, so your real question should be how to execute a function on the mouse down.
Answer by robertbu · Apr 26, 2013 at 02:25 PM
You can test for the MouseDown event and test the position against the Rect you use for your button. Example:
private var rect = Rect(0, ((Screen.height / 2) - 128), 256, 256);
function OnGUI() {
var e = Event.current;
if ((e.type == EventType.MouseDown) && rect.Contains (Event.current.mousePosition))
Debug.Log("Mouse Down");
if(GUI.Button(rect, "Button")) {
Debug.Log("Mouse Up");
}
}
Thanks :), but I get one error. "The name e' does not exist in the current context", could it be that it's javascript and I'm using C#? And it's on this line:
e = Event.current;`
I have changed the rect to Rect rect = new Rect(0, ((Screen.height / 2) - 128), 256, 256);
:-)
I guess I was right, I changed e = Event.current;
to Event e = Event.current;
and now it works perfectly, thank you so much! :D :D :D :D
The code should have been var e since it was Javascript. Event e is right for C#. And you need the 'new' again because you are writing in C# and I posted the example in Javascript.
Answer by Curly-Chick · Mar 10, 2014 at 07:07 PM
Does anyone know the answer in JavaScript. I'd like to hear it!