- Home /
GUI.Button press (not click)
Hello everyone, I want to know if there is a possibility to make a GUI.Button return true when the user immediately press on it, not click (press and release) Am making a game for Android and it's weird to have such an effect with buttons. Am sure there is a simple solution for this, otherwise i had to make it manually
Thank you
Something like the RepeatButton? - You could lock it immediately after the first down state.
Answer by clunk47 · Dec 06, 2013 at 04:52 PM
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
bool pressed = false;
GUIContent btn;
int presses = 0;
void Start()
{
btn = new GUIContent("Test");
}
void Update()
{
if(Input.GetMouseButtonUp (0))
pressed = false;
}
void OnGUI()
{
if(GUILayout.RepeatButton(btn))
{
if(!pressed)
{
presses++;
pressed = true;
}
}
GUILayout.Label(presses.ToString ());
}
}
thank you, but i about this solution and i said maybe there is an "easiest" way to do it ? i thought that just like there is a "ButtonUp" (clicked) test then there must be a ButtonDown test no ?
This is a very simple solution. I'm using a boolean to deter$$anonymous$$e whether or not you've pressed down on the button... This is about the easiest way I can think to do this. You press down, presses is added to once. Otherwise without the boolean, RepeatButton would keep adding to presses as you hold down the button. $$anonymous$$eep in $$anonymous$$d the int presses in this example is just that, example.
Yes i know that this is very simple, as you said it must be the simplest manual way to do it, i just assumed that there must be a build-in function to do that, anyway, i would make this correct answer if you used touch input ins$$anonymous$$d of mouse left click... kidding :p thanx a lot man ^^
LOL! Hey since you mention it, just in case you didn't know, the mouse class works like touch on $$anonymous$$obile ;)
You're very welcome. I believe the mouse commands work, but functions won't, like void On$$anonymous$$ouseDown() won't work as touch, but in Update like in the example above,
if(Input.Get$$anonymous$$ouseButtonDown(0))
Would act like
if(Input.touchCount > 0 && Input.GetTouch (0).phase.Equals(TouchPhase.Began))
Answer by begem0t · Aug 23, 2014 at 04:35 PM
Use GUI.RepeatButton:
if(GUI.RepeatButton(new Rect(0,0,50,20),"Repeat")){
...}
Answer by elblogdelbeto · May 29, 2018 at 09:59 AM
there is an easy way: in the inspector of the button, add component - > event trigger and then you can add there -> add new event type and you cand find the pointer down event