Event Trigger component - how to access input data? (127895)

Hi there,

I’ve been using the Event Trigger component to easily handle events on various 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.

Thanks in advance!

1 Answer

1

After discussing this in the forums (http://forum.unity3d.com/threads/event-trigger-component-how-to-access-input-data.291076/#post-1920576), I got a solution and I’m posting it here for future reference.

By making the function that is called by the Event Trigger require a BaseEventData argument, the information is passed into the function. Then one can cast the BaseEventData parameter as a PointerEventData:

PointerEventData pointerData = data as PointerEventData;

which makes pointerData contain information such as pointer position and delta.

Thanks. I didn't see it at first. The function actually appears in the top of the list when you add the parameter. Working function declaration: public void FunctionName(BaseEventData data) Also it can be used without paramenters. Also @Glorion13 , you may as well accept your answer as correct.

Thanks @Glorion13 and @krosnos , I spent ages trawling through the source code of the EventSystem/BaseInputModule/EventTrigger, working all of this out for myself and then still failed when trying to write a function that would take PointerEventData as a parameter and that would display in the editor (the editor interface was deliberately leaving stuff out). There needs to be better documentation for this. None of this is obvious. All I wanted to do was process a double-click and right click. Neither of these should be this hard.