- Home /
Input.getTouch() vs checking if button is clicked
In my application, I create buttons like this:
if(GUILayout.Button(GUIContent(menuButton.text), menuButton.getGuiStyle())){
menuButton.executeClick();
}
So whenever the button is clicked the executeClick() function is called on it. Now I also have a window behind the buttons that accepts user input and queries on every Update() with
if(Input.touchCount > 0){
var touch : Touch = Input.GetTouch(0);
//use the touch...
When I click a button, the code used to interpret the touch event is executing when I really only want the menu button code to in executeClick(). What's a good way that I can first check if a button is clicked and if none are to use the touch information?
Since it appears that the first section of code is executed before the update function for my window, I put a global boolean that I set to true when the first section is executed, and I check that before allowing the second code to execute. Am I making a bad assumption that the first is always called before the second?
Answer by chaosmaker · Nov 07, 2013 at 09:46 PM
Am I making a bad assumption that the first is always called before the second?
Yes, it is a bad assumption. Execution order is documented here:
http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html
Also consider here it is stated that:
OnGUI: Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.
Why don't you just put your second logic inside first logic:
if(GUILayout.Button(GUIContent(menuButton.text), menuButton.getGuiStyle())){
menuButton.executeClick();
if(Input.touchCount > 0){
var touch : Touch = Input.GetTouch(0);
//use the touch...
}
Or inside executeClick that you can be sure that touch check is made inside(after) the click.
Or else you have to be more clear about what you are trying to achieve that I could provide a more precise help.
Your answer
Follow this Question
Related Questions
Detect UI Button Click Event in Update method 3 Answers
Button.onClick.AddListener 1 Answer
GUITexture OnMouseDown Problem 1 Answer
Force touch to only work on a new touch - buttons 0 Answers
Help with a button 1 Answer