- Home /
(Mobile) Touch anywhere on screen EXCEPT button
Hey I wanna make a mobile game where on the start menu touching anywhere will start the game, but i also want a few buttons to press on the same screen, how do I make it so if you touch a button it doesn't recognize the normal click as well
Answer by TimBorquez · Dec 02, 2013 at 07:18 PM
I am just going with my workaround as I couldn't get anything "legit" to work how I wanted it to
//blank style so screen button doesnt show up
var blankStyle : GUIStyle;
var buttonPressed = false;
function OnGUI () {
if (GUI.Button(Rect(0,0, 500, 500),"Test Button"))
{
buttonPressed = true;
}
//button is screen size and has a blank gui style
//this \/ must be at the bottom so the other buttons can disable its use
if (GUI.Button(Rect(0,0, Screen.width, Screen.height), "", blankStyle))
{
if(!buttonPressed)
{
//do stuff for touching screen
}else
{
buttonPressed = false;
}
}
}
for anyone looking at this later, my solution didn't turn out to work very well. The unity gui on phones is pretty iffy and bad so it misses the top buttons sometimes and only hits the background one...
Answer by Subhajit-Nath · Nov 29, 2013 at 05:11 PM
The simplest way I can think of is to put all the button code in if / else if block.
if (Input.touchCount > 0) //firstly, check if a touch has been made
{
if (GUI.Button(...)) //code for options button
{
// statement
}
else if (GUI.Button(...)) //code for stats button
{
// statement
}
else if (GUI.Button(...)) //code for quit button
{
// statement
}
else //this block will execute if none of the above is true
{
//statement for starting the game
}
}
Didn't check the code, but should be something similar. Hope this helps.
it seems close to a solution but with that way it wont even show the button without touching
Yes, you are totally right... silly me...
Ins$$anonymous$$d you could check the touch count part in the last else if block.
if (GUI.Button(...)) //code for options button
{
// statement
}
else if (GUI.Button(...)) //code for stats button
{
// statement
}
else if (GUI.Button(...)) //code for quit button
{
// statement
}
else if (Input.touchCount > 0)//this block will execute if none of the above is true
{
//statement for starting the game
}
I don't have a touch device or simulator near me right now... :(
With a few modifications that script will work fine:
You list all the buttons and if none of them have been pressed you check if there are any touches:
bool buttonPressed = false;
if (GUI.Button("Button1"))
{
buttonPressed = true;
}
if (GUI.Button("Button2"))
{
buttonPressed = true;
}
if (GUI.Button("Button3"))
{
buttonPressed = true;
}
if(!buttonPressed)
{
if(Input.touchCount > 0)
{
//No buttons pressed, but there has been a touch
}
}
hmm I tried all these solutions and they are still not working, this is turning out to be kind of an annoying problem
I think it's because the buttons register on button up ins$$anonymous$$d of right away...
Without any information on what happens and what exactly is not working, it is kind of hard to provide any more help.
But is the problem that the game starts immediately no matter if you press a button or not? Then maybe checking if the touch was in the release phase will help? Like this:
bool buttonPressed = false;
if (GUI.Button("Button1"))
{
buttonPressed = true;
}
if (GUI.Button("Button2"))
{
buttonPressed = true;
}
if (GUI.Button("Button3"))
{
buttonPressed = true;
}
if(!buttonPressed)
{
if(Input.touchCount > 0)
{
if(Input.GetTouch(0).phase == TouchPhase.Ended)
{
//No buttons pressed, but there has been a touch
}
}
}
Answer by Green-Jungle · Jul 14, 2014 at 09:04 AM
Hello everyone.I tried above code but can't.I'm making a 2d platform game.My character will jump when touch on screen.My problem is when i click on pause game button on screen then Touch still always active.And that make my character automatic jump when click on Pause button.How to my chacracter can't jump when i click on pause button.Please help me.Thanks a lot.
Your answer
Follow this Question
Related Questions
How to make this buttons for Android? 1 Answer
Why this simple code doesnt work? 0 Answers
UI works in editor, but not on mobile device 1 Answer
Swipe menu, problem! 0 Answers
Touch Controls for mobile 1 Answer