Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TimBorquez · Nov 29, 2013 at 04:36 PM · androidbuttonmobiletouchmouseclick

(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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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;
        }
     }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimBorquez · Dec 14, 2013 at 02:59 PM 0
Share

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...

avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimBorquez · Nov 29, 2013 at 06:13 PM 0
Share

it seems close to a solution but with that way it wont even show the button without touching

avatar image Subhajit-Nath · Nov 29, 2013 at 06:23 PM 0
Share

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... :(

avatar image GameVortex · Nov 29, 2013 at 06:28 PM 0
Share

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
    }
 }
avatar image TimBorquez · Nov 29, 2013 at 06:47 PM 0
Share

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...

avatar image GameVortex · Nov 29, 2013 at 06:55 PM 0
Share

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
        }
    }
 }
Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges