Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by yotamr8 · Aug 20, 2018 at 11:51 PM · mobilecanvasmenu screen

Detect clicks on the screen but not on any button

Hi! I am developing android game and I want to display a menu (a canvas) that includes buttons. To get out of the menu and start the game, the user needs to tap anywhere on the screen. Now, in order to do so, I need to detect when the user clicks on a button (In that case, the game won't start) and when he clicks on the screen, without touching any button. There are also some text object on my canvas, Clicking on them is considered a click on the screen. Before I added the buttons to the canas, I had just checked on each update if there is a new Touch (using Input.GetTouches() -> touch.begin). In that case, after I had added the buttons, when a button was clicked, the touch was detected and the menu disappeared. How can I know when the user clicks on the buttons, and when he clicks on the rest of the screen? Thank a lot!

Comment
Add comment · Show 1
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 toddisarockstar · Aug 21, 2018 at 03:08 AM 0
Share

why not simply make all the buttons set a bool variable to true. then when you get your overall screen touch only apply it if the bool from the buttons is false?

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by saschandroid · Aug 21, 2018 at 06:59 AM

You probably want this: EventSystem.IsPointerOverGameObject

     // Check if there is a touch
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
     {
         // Check if finger is over a UI element
         if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
         {
             Debug.Log("Touched the UI");
         }
     }
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 PerfectSkies · Mar 21, 2019 at 03:08 AM 0
Share

This works both ways.... To detect touches on button bit not screen: if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { // Check if finger is over a UI element if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) { Debug.Log("Touched the UI"); } else { //Code goes here } }

avatar image
0

Answer by Reivaj28 · Aug 21, 2018 at 09:39 AM

You have two scripts, one for your button and the other for a GameObject that occupies the whole screen. When the button is clicked, is called the OnClick() method. Now, for making the Screen clicked you must do the following thing in the script of the GameObject:

 public class ScreenScript : MonoBehaviour {
 
      private void OnMouseDown() {
          //Do something when the screen is clicked
      }
 }
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

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

World canvas with screen space canvas performance mobile ? 0 Answers

How do I detect touch on another object and check by tag/name? 1 Answer

The UI doesn't show when i click a button and the first page(the one with the title and buttons) doesn't move when i click 1 of the buttons 1 Answer

How do you make a pause menu? 0 Answers

how to make a menu open and close with the same button 3 Answers


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