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 :slight_smile:

Actually GUI buttons execute on mouse up, so your real question should be how to execute a function on the mouse down.

2 Answers

2

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.

Comment as Answer: Please don't post comments in the answer area. Answers are for solutions to the problem. Post comments by selecting the [add new comment] link on the right bottom of the question and also on the bottom of each answer. Note comments do not go through moderation and will appear immediately.

Does anyone know the answer in JavaScript. I’d like to hear it!