- Home /
How to detect a button being released?
I am using JavaScript and I have a GUI button. When the button is held down a timer goes and when it is released the timer stops. I'd like to have it so When the mouse releases the button the game would stop, show a game over and your time would be displayed.
I tried GetButtonUp function but that doesnt work with GUI. Is there a way to make buttons without GUI ? pragma strict
var counter: int = 0;
var Timer = 0.0;
var gameText: GUIText;
var btnStyle:GUIStyle;
var backGround:GUIStyle;
function Start(){
gameText = gameObject.GetComponent(GUIText);
}
function OnGUI () {
GUI.Box (Rect (100,100,200,300), "Hold IT", backGround);
if (GUI.RepeatButton(new Rect(50,50,100,110),"", btnStyle)) {
counter++;
Timer += Time.deltaTime;
var hours : int = Timer / 3600;
var minutes : int = Timer / 60;
var seconds : int = Timer % 60;
var fraction : int = (Timer * 100) % 100;
gameText.text = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}", hours, minutes, seconds, fraction);
}
}
You could use a GUITexture with a 2DBox collider on it. Then you could use the On$$anonymous$$ouseDown() and On$$anonymous$$ouseUp() functions to create your own button rather than using the OnGUI() function's inbuilt buttons.
Another alternative is to use a third party GUI system. Unity UI (available in the 4.6 beta) and NGUI (Asset store) are both popular options
Answer by Fmressel · Oct 14, 2014 at 11:55 PM
If I'm understanding your question correctly, I would do something like this:
var counter: int = 0;
var Timer = 0.0;
var gameText: GUIText;
var btnStyle:GUIStyle;
var backGround:GUIStyle;
//A bool to let us know if the button has been pressed or not
var down = false;
function Start(){
gameText = gameObject.GetComponent(GUIText);
}
function OnGUI () {
GUI.Box (Rect (100,100,200,300), "Hold IT", backGround);
if (GUI.RepeatButton(new Rect(50,50,100,110),"", btnStyle)) {
//when the button is pressed we make down equal true
down = true;
counter++;
Timer += Time.deltaTime;
var hours : int = Timer / 3600;
var minutes : int = Timer / 60;
var seconds : int = Timer % 60;
var fraction : int = (Timer * 100) % 100;
gameText.text = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}", hours, minutes, seconds, fraction);
}
else if(down == true && Event.current.type == EventType.MouseUp) {
//An else if statement that will only be true if the button is not
//pressed but has been pressed in the past and the mouse has been lifted
//but to check in the OnGUI function if the mouse has lifted we need to use Event.current and
//check it against the event type of mouse up which will only be true if the mouse has been pressed down and released
//Thanks to BoredMormon for the Event.current == EventType.MouseUp part! This code wouldn't work without it!
//Game over code
//Remember to set down to false every time a new game is started or else
//the game over code will run right at the start of the game!
//Also remember that if you don't set down to false after the game over code every time you click the mouse and release the game over code will run
}
}
Here is some more info about Events:
http://docs.unity3d.com/ScriptReference/Event.html
Hope that helps! Let me know if it didn't answer your question!
Just note that this will not work using OnGUI as written. The immediate mode GUI framework calls OnGUI multiple times per frame, with various events. You need to implement event checking for this to work.
Change line 25 to the following for this code to work
else if(down == true && Event.current == EventType.$$anonymous$$ouseUp)
Sorry for the late response ! So I added the new line of code for the event but I get this error
Assets/GUITest.js(25,43): BCE0051: Operator '==' cannot be used with a left hand side of type 'UnityEngine.Event' and a right hand side of type 'UnityEngine.EventType'.
@rondonron: There's a typo ;) It should be:
else if(down == true && Event.current.type == EventType.$$anonymous$$ouseUp) {
I'll fix it in the answer...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I stop getting mouse position of Ui Elements? 0 Answers
rewrite script 1 Answer
[Unity 4.6] UI Button problem null reference exception [Js] 0 Answers