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

Passing Input events through to objects behind them

Discussion in 'Scripting' started by UnbridledGames, Sep 26, 2020.

  1. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    I have invisible bars on either side of my screen that have scripts attached that detect swipes starting, so that the user can edge-swipe to the next screen, similar to how you page through screens on a mobile device.

    The problem is that these bars also partially cover some buttons that need to receive clicks. (and if they are narrow enough to not cover the other UI elements, they are TOO narrow to reliably detect the swipe)

    I've have other scrips which have passed the event data through to other objects/scrips behind it - I know how to do that. The issue is - in that case, I KNOW whats behind the object thats checking for a swipe. Its an item bar that detects a left swipe to delete, and if it's not a swipe, well I pass the event data on through to check for clicks etc. However, for these side bars... whats behind where you start swiping (or more to the point, not swiping, and you are clicking/tapping) might be nothing, might be one of three buttons, or if you're on another page alltogether, might be something else.

    So the issue is I don't know WHAT to pass the event through to...

    Is there an eloquent way to handle this? I cant just pass the event to whatever COULD be behind it, thats excessive. I had thought of firing off a raycast at that point where the screen was touched and somehow using that, but that also feels clunky.

    Is there some way to do this that I'm not thinking of? Ideally, it would be something like:
    if(thisStartedASwipeAndNotAClick == true) {
    doTheSwipingStuff();
    } else {
    allowTheTapToPassThroughToAnythingThatMightBeBehindMe();
    }


    Hoping theres a somewhat simple solution for this...?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    Could you put swipe area behind the buttons, rather than in front of them?

    (Or, if it's important that swipes are detected even when they start on top of buttons, could you make the buttons be children of the swipe area? Input events that aren't handled by an object are automatically passed up the object hierarchy.)
     
    UnbridledGames likes this.
  3. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    Hmm. I honestly hadn't thought of that. It always made sense for the bars to be "on top" but if they were behind other objects, and still worked, that would be perfect. I'll give it a shot tomorrow and see how it works. The way you describe it SHOULD work... some items require a click/tap... starting a drag is NOT a tap, so if it's passed up the hierarchy, it should work?

    Wow if thats the solution I'm gonna feel silly not thinking of it. :)
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    That is the standard way that Buttons inside of a ScrollRect work--hover and click events are captured by the Buttons, but drag events pass up to the ScrollRect.

    You may want to check your drag threshold (that's a parameter in the input module that determines how far the pointer has to move before something counts as a "drag"). My recollection is that Unity's default value works well for a mouse but is pretty sensitive for a touchscreen, often causing taps to unintentionally turn into drags. I think I multiplied it by a value of 4 for mobile.
     
    UnbridledGames likes this.
  5. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    Omg now I feel dumb. Ok, so the touchbar parent object was a sibling of the main UI, not in the parent/child relationship at all. I moved them into the UI and made my swipe panels a child of it. Everything works perfectly. Mouseover works on the buttons, and if I swipe from there the screen moves instead of the button clicking. Thanks a million for the insanely simple solution.

    Also thanks for the heads up on the drag threshold. I do remember seeing that setting in the past, but if and when I ran into the issue you described, I'd be going crazy trying to find it, if I even remembered it at the time.