- Home /
Alternative to GetButtonUp for GUI.Button
I need a system where I click an hold a GUI button, something charges, and when i let go of the button, do something else? There doesn't seem to be a simple piece of code in the reference for such a basic problem. It's ridiculous. If you can get ButtonDown and ButtonUp for physical keys, why not for the GUI?
A hint, this is for android.
Would GUI.RepeatButton be of some help?
http://docs.unity3d.com/Documentation/ScriptReference/GUI.RepeatButton.html
Answer by whydoidoit · Jan 15, 2013 at 09:49 AM
How about this?
var buttonDown : boolean;
function OnGUI()
{
if(GUILayout.Button("Press Me"))
{
buttonDown = true;
DoSomething();
}
else if(buttonDown)
{
buttonDown = false;
DoSomethingElse();
}
}
Unfortunately not. I need a value to increase over time while the buttons is held down, and be able to be released at any point.
Isn't that just
if(GUILayout.RepeatButton("Press $$anonymous$$e"))
{
someValue++;
buttonDown = true;
}
else if(buttonDown)...
But repeat button would increment it, and once the bool is true, apply the number. I need it to only apply once it has been let go.
But that's why I had the else if (buttonDown) you would only apply it then...
So you're right, you have to check the up event only on repaint - this does the Debug.Log only when the button is released.
using UnityEngine;
using System.Collections;
public class TestButton : $$anonymous$$onoBehaviour {
int powerUp;
bool buttonDown;
void OnGUI()
{
if(GUILayout.RepeatButton("Hello"))
{
powerUp++;
buttonDown = true;
}
else if(buttonDown && Event.current.type == EventType.repaint)
{
buttonDown = false;
Debug.Log(powerUp.ToString());
}
}
}