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
1
Question by LeroyTH12 · Jan 03, 2015 at 05:59 PM · uimobiletouch

UI buttons and touch input problem

Guys, I have one problem. I'm developing a mobile game which is a simple infinite runner. For player controls I using something like:

 if (Input.touchCount > 0)
 {
     for (int i = 0; i < Input.touchCount; i ++)
     {
         Touch touch = Input.GetTouch(i);
         if (touch.phase == TouchPhase.Began)
         {
             //Jump
         }
     }
 }

The pause button is a UI element and when I'm pressing it player jumps but he is not supposed to. Then I've made a check with EventSystemManager.currentSystem.IsPointerOverEventSystemObject() in player code. And while testing with Unity Remote it works fine, but after building project on Android device this doesn't work, player still jumps. How to solve this problem? Maybe there is another way of checking?

Comment
Add comment · Show 4
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 Qasem2014 · Jan 04, 2015 at 06:57 PM 0
Share

i have same problem ! do you solve it ?

avatar image LeroyTH12 · Jan 05, 2015 at 05:05 AM 0
Share

Well, yeah. I found another solution. Ins$$anonymous$$d of using Input.touch, I made an invisible UI button stretched to full screen and added the following script to it:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 public class PlayerInputArea : $$anonymous$$onoBehaviour, IPointerDownHandler, IPointerUpHandler {
 
     private bool touched;
     private int pointerID;
 
     void Awake () {
         touched = false;
     }
 
     public void OnPointerDown(PointerEventData data) {
         if (!touched)
         {
             touched = true;
             pointerID = data.pointerId;
         }
     }
 
     public void OnPointerUp(PointerEventData data) {
         if (data.pointerId == pointerID)
             touched = false;
     }
 
     public bool IsTouched() {
         return touched;
     }
 }

And then I use IsTouched() in player code. It works fine. But one thing to note: this UI button must be behind all other buttons.

avatar image Attrom LeroyTH12 · Apr 21, 2016 at 01:40 PM 0
Share

I had the same issue. I created the full-screen button that triggers the gameplay action, I created the pause button and my pause button wasn't working. I came across your post and set the Z coordinates of the buttons so that pause button is in front of the action button, but it didn't work out. What worked out eventually was to set the hierarchy order so that action button is first and pause button is second, and it worked.

avatar image Qasem2014 · Jan 05, 2015 at 06:26 PM 0
Share

thanks for reply ;) i should try it :)

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Krizzen · Jan 04, 2015 at 11:17 AM

I ran into this exact issue today. Apparently EventSystemManager.currentSystem.IsPointerOverEventSystemObject() always returns false in an actual APK build. The issue tracker was marked "By Design", but it definitely seems like a bug to me.

Use this code as a work around (Credit goes to MWK888, found here: http://forum.unity3d.com/threads/ispointerovereventsystemobject-always-returns-false-on-mobile.265372/ ):

 /// <summary>
 /// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
 /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
 /// </summary>
 private bool IsPointerOverUIObject() {
     // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
     // the ray cast appears to require only eventData.position.
     PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
     eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  
     List<RaycastResult> results = new List<RaycastResult>();
     EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
     return results.Count > 0;
 }
  
 /// <summary>
 /// Cast a ray to test if screenPosition is over any UI object in canvas. This is a replacement
 /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
 /// </summary>
 private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition) {
     // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
     // the ray cast appears to require only eventData.position.
     PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
     eventDataCurrentPosition.position = screenPosition;
  
     GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
     List<RaycastResult> results = new List<RaycastResult>();
     uiRaycaster.Raycast(eventDataCurrentPosition, results);
     return results.Count > 0;
 }


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 LeroyTH12 · Jan 04, 2015 at 11:47 AM 0
Share

Thanks for your reply, $$anonymous$$rizzen. I'll try to do something with this.

avatar image
0

Answer by hdtnl · Feb 01, 2017 at 11:53 AM

Let try it from Unity Document: https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

 void Update () 
     {
         // 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 · 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
0

Answer by DimitryCastex · Mar 25, 2016 at 09:32 PM

For touch input this code work for me

 /// <summary>
     /// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
     /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
     /// </summary>
     private bool IsPointerOverUIObject(Touch t)
     {
         // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
         // the ray cast appears to require only eventData.position.
         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
         eventDataCurrentPosition.position = new Vector2(t.position.x, t.position.y);
 
         List<RaycastResult> results = new List<RaycastResult>();
         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
         return results.Count > 0;
     }

the argument t is an element in Input.touches[]

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

7 People are following this question.

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

Related Questions

Pressing two buttons simultaneously with the same touch 2 Answers

Mobile Multiplayer touch controls 0 Answers

UI Buttons Working on Android. 2 Answers

Do UI Buttons work the same for Touch for an Android Game? 1 Answer

UIButtons and UITexts are on the good position when I run the app with Remote4 but when I install .apk it's totoally different. How to solve it? 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