Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to stack raycast events in Screenspace UI?

Discussion in 'Editor & General Support' started by MrLucid72, Mar 16, 2020.

  1. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    962
    I have 3 stacking UI elements in a Screen Space Canvas using the Event system:

    upload_2020-3-16_22-55-52.png

    While the click (blue) and hover (red) handlers seem to work fine since they aren't stacking with each other, adding the IScrollHandler "hotspot" (green) prevents anything below the green layer from working.

    * I can layer the green layer BEHIND these other objs, but it makes scrolling super awkward in only certain areas around the objs.

    * I can layer the object IN FRONT of these objs, but then only the scroll works and nothing else works.

    How can I have these all work together, as if raycasting was off but the event was still triggered: I'd like the green IScrollHandler gameObj to stack with all raycasted layers below it. How can I accomplish this? My hierarchy looks like this:

    -SomeBtn // Won't click
    -SomeHover1 // Won't hover
    -SomeHover2 // Won't hover
    -ScrollHotspot // Eats up the above raycasting events. If I were to !raycasting, the others work again. If I were to drag it above them, they all work but the ScrollHotspot is awkward and feels buggy since it's not a perfect square area.

    TL;DR: I essentially want stacking raycast events to NOT be blocked by my top layer (as if I turned OFF raycasting for that layer, but somehow still allowed the attached Scroll event to continue). I want to allow BOTH a square field ScrollHotspot && the hover/click events inside (stacking [graphic?] raycasting).
     
    Last edited: Mar 16, 2020
  2. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    962
    Related:
    1. https://forum.unity.com/threads/detect-drag-but-pass-through-all-other-events.266594/
    2. https://forum.unity.com/threads/passing-an-event-through-to-the-next-object-in-the-raycast.266614/
    EDIT:
    Came up with a ghetto solution based off some ideas here (using MEC for coroutines; you can swap this out):

    https://pastebin.com/djEKfWDy

    This example is to stack scroll event with others, but it's easy to swap it with drag, for example:



    Throw ScrollEventPassThru in a component that is triggered on top and drag in the parent component you want to trigger that usually doesn't. Baddaboom! Pass-through. Sorta efficient, too, since you direct it to THE obj that should contain the event. You can easily set the option to pass up/down, alternately. Just swap it out.
     
    Last edited: Mar 17, 2020