Search Unity

Event Trigger component - how to access input data?

Discussion in 'UGUI & TextMesh Pro' started by Glorion13, Jan 14, 2015.

  1. Glorion13

    Glorion13

    Joined:
    Nov 19, 2012
    Posts:
    8
    Hi there,

    I've been using the Event Trigger component to easily handle events on various UI elements and GameObjects. When simply wanting to call a function on an event triggering, I can just link the script and choose the function. If I want the function to require some parameters, I change the C# function definition and then add the parameter on my Event Trigger component.

    My question is, what if the parameter I want to pass to my function is input data, such as the delta of the mouse or finger being dragged across the screen? Specifically, I have an Event Trigger Component with a Drag event which calls a function on a script. I want to access the Drag input data in that function.

    When I'm running my game and I select the EventSystem in my scene, I can see in real time all the numbers changing under the "Intercepted Events" window. But how can I actually use these within my function?

    Thanks in advance!

    (I've also created a question about this, here http://answers.unity3d.com/questions/875535/event-trigger-component-how-to-access-input-data.html)
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    If you look at the header of the type you are trying to use with the even trigger (pointerEnter, PointerExit, ect) you will see that there is a (baseEventData) that gets passed automatically. you then case that to what you need.
     
  3. Glorion13

    Glorion13

    Joined:
    Nov 19, 2012
    Posts:
    8
    Thanks for your quick reply! :)

    I might be missunderstanding the BaseEventData class but from what I can see it contains no information about pointer position etc.

    Could you give me an example of how to get it?

    Thanks!
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    BaseEventData is just the base class which other EventData classes derive from. You need to cast it to a PointerEventData. But be sure to check its of that type first as the type depends on the call you implement and i dont know what calls your using.
     
    Orrib and Glorion13 like this.
  5. Glorion13

    Glorion13

    Joined:
    Nov 19, 2012
    Posts:
    8
    I just wanted to confirm that casting the BaseEventData to PointerEventData works like a charm!

    Thanks very much! :)
     
  6. Moxid

    Moxid

    Joined:
    Jan 24, 2016
    Posts:
    51
    Works very good!
     
  7. Dewald_Bodenstein

    Dewald_Bodenstein

    Joined:
    Feb 27, 2014
    Posts:
    6
    So this is old, but what if you want to pass a second parameter to the event handler together with the event data? Simply adding another parameter seems to break the handler and it doesn't even appear in the list of options in the Editor UI.
     
  8. Glorion13

    Glorion13

    Joined:
    Nov 19, 2012
    Posts:
    8
    Hey! :) Hmm could you provide a bit more information about what you're trying to achieve, and why you want to pass a second parameter?

    The way I understand it (it's been a while now), this function would anyway intercept things from the EventSystem, which I don't think would be sending your other parameter anyway.
     
  9. Dewald_Bodenstein

    Dewald_Bodenstein

    Joined:
    Feb 27, 2014
    Posts:
    6
    Hay Glorion, thanks for the reply.

    In my case I have an avatar with an Event Trigger script on it. On pointer click event I want to call a function on a UI object and pass in the clicked avatar with the event data so I can check which button was used before reacting to the click event.

    Know a better way to do this other than just creating a script to place on the avatar? It doesn't make sense to have the Event Trigger script then at all.

    [EDIT] Found a solution! So I passed in the BaseEventData into my event handler function, then cast it to PointerEventData. This class has a hovered property which contains the avatar clicked on. :)
    Docs here: https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.PointerEventData.html
     
    Last edited: Nov 22, 2021