• 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 /
  • Help Room /
avatar image
11
Question by ecesis_llc · Sep 30, 2015 at 06:03 PM · androiduitoucheventsystem

Android touches pass through UI elements

Is there a way to prevent Android touches to pass through UI elements on a canvas? I'm using Unity 5.2.1f1.

Possible answers for non-tile map uses http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.html

Setup

  • 2d game.

  • An overlay canvas with buttons.

  • A gameobject input handler that checks for world input on the update method. Input through this method checks the world for interactivity.

Why do I check for world objects based at a location? I'm using a tile map that dynamically loads tiles and the system has a simple method to convert a world location to a specific tile location on the map. The tiles do not have any events on them.

Desktop and Editor Work

Grab from the update method Input.mousePosition if the pointer is not over a game object.

 if(    EventSystem.current.IsPointerOverGameObject() )
         return;

Android Build Doesn't Work

Grab the first touch. If it has ended check if the pointer is over an object.

 Touch touch = Input.GetTouch(0);
 
             if( touch.phase == TouchPhase.Ended )
             {
                 //prevent touch through
                 if(    EventSystem.current.IsPointerOverGameObject(touch.fingerId) )
                 return false;
 
                 vec = touch.position;
                 return true;
             }

I've also tried looping over touches. IsPointerOverGameObject appears to be useless for preventing Android touches from passing through UI canvas objects.

Android Build Work Around

The only way I've found how to work around this issue, thanks to Fattie, is to check for a selected gameobject in the event system.

 if( EventSystem.current.currentSelectedGameObject != null ) return false;

Is there a better way to do this? The only tutorials I've seen that might relate require an event interface for the tiles in the tile map and any game object agents acting "on top" of the tile map.

I've spent about my whole afternoon reading forum posts about this issue and have not come way with a better solution.

Some discussions

http://forum.unity3d.com/threads/ui-not-working-5-1-1-5-2-0b1.339462/#post-2195414

http://forum.unity3d.com/threads/upgrading-to-unity-5-2-ui-problem-with-raycast-target.353586/

http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.html

http://gamedev.stackexchange.com/questions/90196/how-to-detect-that-user-has-touched-ui-canvas-in-unity-4-6

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

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kitty001 · Sep 30, 2017 at 03:02 PM

Because the forums seem filled with half solutions and I had to search for hours to fix it my solution is as follows:

 if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject () == false) {
               currentDestination = clickPoint, 
     }

So the Raycaster only sets click point to be the current destination if the point is not pointed at a "Game Object" i.e. a UI element. Inside of a switch statement, it works well on Android build.

Hope it helps, Kit

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 Tom-Mensink · Oct 23, 2018 at 08:59 AM 0
Share

The whole forum issue is about that this does not work for Android, so how could it for you? What does work for me is on of the answers above

EventSystem.current.IsPointerOverGameObject() || EventSystem.current.currentSelectedGameObject != null

  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

72 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

Related Questions

UI button events not working on android 3 Answers

What is the best way to implement a more responsive custom drag event with Scroll Rect? 0 Answers

Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android) 2 Answers

How to pick up an object when touching a button(Mobile) 0 Answers

Spawning enemies after death help 0 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