Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Touch input clicking through UI! Is there no fix?!

Discussion in 'Scripting' started by visualhippocracy, May 26, 2021.

  1. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    I'm baffled with this problem? How can Unity be considered a serious game engine for developers when such a simple thing is not hardcoded into the system, or available as a toggle etc.?!

    I made a ui but clicks would pass through them. So I enabled a fix that I saw on various youtube videos about using system event to detect if UI is detected.
    this following code actually:
    Code (CSharp):
    1. if (EventSystem.Current.IsPointerOverGameObject())
    2. return;
    This worked in the editor but doesn't work on any phones in the android builds that I make.

    The hell is going on?!
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    272
    First of all, what are you trying to do? I never really used EventSystem in my games, I never needed it, there a plenty of other ways to do any android input functionality. Again, what do you need?
     
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    IsPointerOverGameObject needs to know which pointer it is supposed to look for
    by default if you dont give any input parameter it will assume the mouse

    do
    IsPointerOverGameObject(Input.touches[0].fingerId) (or Input.GetTouch(0) instead of Input.touches[0]) to check it for the first touch on the screen, note that the second touch will still go through with this though, but that shouldnt matter for single touch application

    (note: befor doing this check that Input.touches has at least one element in it, otherwise it will throw errors whenever nothing touches the screen)
     
    Last edited: May 26, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Grab some coffee, take a deep breath and sit down and tell us what is truly on your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you're looking to block touches to covered-up UI, this already happens for RaycastTarget objects, and you can cause it to be the whole screen by making an invisible (or visible) graphic that covers the whole screen.

    Nothing baffling at all, I do it all the time with every single one of my popup windows. You can even see an example of it my Datasacks package: run the
    game1
    scene and bring up the
    popup1
    window, and see how I cover the entire screen.

    Datasacks is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/datasacks

    https://github.com/kurtdekker/datasacks

    https://gitlab.com/kurtdekker/datasacks

    https://sourceforge.net/projects/datasacks/
     
  5. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    Ok, so I've created a game where there are multiple tiles which rotate when touched.
    I've setup a
    private void onmousebuttondown
    Which is controlling rotation on either left click (or in case of android on touch).
    I've setup a ui for the pause menu which is overlayed on top of the game. But when paused if I click any button on the pause menu the tiles rotate.
    I want to stop this from happening.
    I've tried using the following methods:
    1. Detecting whether mouse cursor is over ui object or not (this works in editor but not on android)
    2. I tried the get touch method as well but it throws an "out of bounds error" so nothing works in the editor. But on Android the game runs but UI click through still happens.
     
  6. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    272
    Try to implement interface for your gameObject script:
    IPointerUpHandler (trigger on release),
    IPointerClickHandler (trigger on release inside bounds) or
    IPointerDownHandler (trigger on press).
    These things always worked for me, UI overlay will prevent underlying layers from triggering.

    Or simply make check in tiles script, for example "if (Time.timeScale == 0) return" if you are using timeScale for pause. Or make some global state flag "gameIsPaused".
     
    Last edited: May 27, 2021
    Munchy2007 likes this.
  7. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    So I jad asked this question else where as well and they managed to give me a line of code that they were using themselves which worked fantastic for me.
    This is the code:
    Code (CSharp):
    1. private bool ClickedOnUi(){
    2.  
    3. PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    4.         eventDataCurrentPosition.position = Input.GetTouch(0).position;
    5.         List<RaycastResult> results = new List<RaycastResult>();
    6.         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
    7.         // return results.Count > 0;
    8.         foreach (var item in results)
    9.         {
    10.             if (item.gameObject.CompareTag("UI"))
    11.             {
    12.                 return true;
    13.             }
    14.         }
    15.         return false;
    16. }
    17.  
    18. private void OnMouseDown(){
    19. if (input.touchCount > 0){
    20. if (ClickedOnUi() return;
    21. }
    22. // Rest of my code
    23. }