Global Listener results in Event beeing executed twice.
Hey,
Let's say we have a simple cube and we want it to change its color when a tap gesture is performed. First thing to do would be to add a script to the cube which implements the IInputClickedHandler-Interface to do just that (change the color).
If I want to recognize any tap in the scene to change the color of the cube, I just add the "SetGlobalListener"-Script to the cube and it works just fine.
One thing which strikes me though is, that when I focus the cube and perform the tap gesture the "OnInputClicked"-Function is executed twice. Once from the GlobalListener and once from the cube itsself.
I know, that I could create an empty GameObject and attach the "ColorSwap"-Script and the "SetGlobalListener"-Script to that one. Then I would have to pass a reference to my cube to the script and it works. But I think it is better (i.e. easier to understand and maintain), if all scripts changing the cube are attached to the cube and not to some other GameObject.
Are there some changes we can make to the InputModule to avoid the OnInputClicked-Function being executed twice?
Thanks in advance
Global listener are global before everything so you try to set a flag in you global listener and check it on the script attached to the cube, if the listener has already handled the event do nothing, otherwise execute as normal.
You can also modify the code in the holotoolkit to make the global listener behave as you describe, the code to handle this is in HandleEvent function in InputManager, you can add a modify it to add a check on the focused object part so that the event is not fired if the object is a in the global listener list.
@ignavus : Thank you for your answer.
I actually did the following now:
public void OnInputClicked(eInputClickedEventData eventData)
{
if (!eventData.used)
{
//Do stuff...
eventData.Use();
}
}