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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generic solution for passing events through to the next responder?

Discussion in 'UGUI & TextMesh Pro' started by greay, Sep 30, 2016.

  1. greay

    greay

    Joined:
    Mar 23, 2011
    Posts:
    88
    I've searched all over like crazy, and while this question does seem to come up a lot, every single answer I've found has been either incredibly specific to the asker's particular problem (and thus useless for me), or incredibly vague, like "look into Canvas Groups".

    The default behavior is alright, for most situations, but is there a generic way to, after handling an event, to let the next thing in line still respond to events? The CanvasGroup's "block raycasts" property doesn't work at all because then the object doesn't respond to any events.

    The best I've found is "write your own input module" but that's a pretty hefty undertaking. And since this seems to come up a lot, I'm hoping there's an existing solution that's general-purpose.
     
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    i dont recall anyone mentioning a feature like that for unityUI...
    ...but i did add something experimental to kissUI, my own UI, in development.

    ex: Image can Handle Input. But if the option is enabled.. wont consume the event, passes it along.
     
    eses likes this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @greay

    Hi there,

    yes, it seems there is very little info about this. Many things related to UI that are not out of the box available seem to be bit too hard to do IMHO.

    However, there is execute events.

    I cooked up a simple example (well, only one I know) that relays button click to all buttons below. I think you could make it be any Selectable instead of Buttons and you could just relay the click to one item below, and maybe it too has the same script, and the it would relay click once again - or not.

    However, even though this will relay the click, you won't get visual feedback, I think it could get messy to somehow manage spriteStates and button colors when they are pressed, highlighted and then when mouse button for example is released, all the stacked buttons would be returned to normal state... maybe someone has a better grasp of this.

    Code (csharp):
    1.  
    2. public class ButtonRelayClick : MonoBehaviour, IPointerClickHandler
    3. {
    4.    public void OnPointerClick (PointerEventData eventData)
    5.    {
    6.      List<RaycastResult> list = new List<RaycastResult>();
    7.      EventSystem.current.RaycastAll(eventData, list);
    8.  
    9.      for (int i = 0; i < list.Count; i++)
    10.      {
    11.        Button button = list[i].gameObject.GetComponent<Button>();
    12.  
    13.        if (button != null && button.gameObject != this.gameObject)
    14.        {
    15.          GameObject item =  list[i].gameObject;
    16.          ExecuteEvents.Execute<IPointerClickHandler>(item, eventData, ExecuteEvents.pointerClickHandler);
    17.        }
    18.      }
    19.    }
    20. }
    21.  
     
  4. tomglenn

    tomglenn

    Joined:
    Aug 3, 2015
    Posts:
    28