- Home /
GUI RepeatButton can only be clicked once
What i need is a GUI repeat button that only can be clicked once. I use the Gui button for a Gui power bar thats indicates how much power you use.
void OnGUI() {
bool eindeKlik = true;
bool maakBarkleiner;
maakBarkleiner = false;
if (GUI.RepeatButton( new Rect(40, 40, 100, 50), "Lift off!") == true)
{
maakBarkleiner = eindeKlik;
eindeKlik = false;
}
else{
maakBarkleiner = eindeKlik;
}
if(maakBarkleiner == true)
{
damage = +0.15f;
ApplyDamage();
print(eindeKlik);
}
so you mean it can be clicked only once then it disapears or u click and nothing happens?
No, here is a example of what i wanted http://www.unity3dstudent.com/2011/01/worms-style-power-bar/ but then with a gui button.
So when I hold the gui button the powerbar charges when i release the gui button the object going to fire something.
Answer by ziptao · Jan 06, 2014 at 04:51 PM
I think you need some sort of way to tell how long the mouse button has been pressed down, and add to the power level with that. Then once the mouse button is released, you can use a method to fire based on the power gained while the button was down.
if (Input.getMouseButton(0))
{
//add amount to power level every second or however often you want
//when mouse is released
if (Input.getMouseButtonUp(0))
{
//fire something
}
}
This is the way I would go about something like this. You would also have to have a way to make sure the mouse is on the button and not just anywhere on the screen, so I would test for that as well. You might have to play around with it for a little bit, but it should work in a similar way to this.
Answer by haitam1 · Jan 12, 2014 at 05:39 PM
the solution is:
//checks where the mousebutton position is
public Vector2 mousePosFromEvent = Event.current.mousePosition;
//the rect where the mouse position must be to use the function mouse up
public Rect eersteButton;
//release bool stand for now on false because it needs to be just pressed one time
public bool laatlos = false;
void OnGUI() {
//describe the size and position of the button rect
eersteButton = new Rect(10, 70, 100, 100);
//if mouse is on the current rect(button)
if(eersteButton.Contains(Event.current.mousePosition))
{
//if mouse up in the current rect(button)
if(Input.GetMouseButtonUp(0))
{
//realeas = true
laatlos = true;
print(laatlos);
}
}
//define the button to show up and if clicked
if(GUI.RepeatButton(new Rect(eersteButton), "button1"))
{
//checks if the button isn't released before and if the mouse is on te current rect position/size (button)
if(eersteButton.Contains(Event.current.mousePosition) && laatlos == false)
{
damage = +0.15f;
ApplyDamage();
}
}
}
maybe there is an easier option to do this but this works fine for me (it also works on a mobile phone)
Your answer
Follow this Question
Related Questions
Making a button desplay after pressing a button? 2 Answers
Unity GUI Style with if Statements 1 Answer
Conversion method types(void to bool)[SOLVED] 1 Answer
If button clicked statement? 1 Answer
Gui Playing Audio Trouble 1 Answer