- Home /
How do I detect when UI events are triggered on *any object*?
I want to know when UI events are being triggered so I can intercept them and inject my own code.
To do this, I planned to add my own script to the object that has the EventSystem and StandaloneInputModule components, and that would use EventTriggers. However, I didn't realise that EventTriggers need to be attached to the actual object that's being manipulated (i.e. on each and every button in the canvas).
Instead, I want to add a 'global detection system' that tracks whenever navigation directions are pressed or any button is clicked, with "Submit" or "Cancel", then fire off some events as I need to.
Thanks!
Answer by xxmariofer · Feb 06, 2019 at 03:09 PM
why you need to do this? isnt easier to create custom buttons that fire your methods? i mean if you need to inject your own code this is the easier solution. something like
button.onClick.AddListener(() => ButtonClicked());
and create a method called ButtonClicked with all the code you want, you just need to add that listener to each button you want.
To add some context, I'm trying to create a workaround for the problem I detailed here: https://forum.unity.com/threads/ui-navigation-breaks-if-you-deselect-all-objects-with-mouse-or-touch.624679/
Essentially the problem is: if you mouse click or touch in empty space, all buttons become deselected and you can't re-select them using keyboard/controller alone (you have to continue using mouse/touch).
The buttons all work fine individually, and navigation is fine until you change input method. If the player accidentally clicks or touches without realising it, the entire UI system effectively breaks for them. I observed a play-tester do this and they thought the game had crashed.
I'm trying to add some code that will detect that the player is trying to continue using keyboard/controller when nothing is highlighted, and then subsequently re-highlight the last highlighted object.
ok sorry i missunderstood you. you want that when the player clicks the button, it gets highlited again, but not fired right? the only thing i can think of is adding a script to each button, implementing a the onselect method https://docs.unity3d.com/ScriptReference/UI.Selectable.OnSelect.html storing a reference to the last selectable ui button in a uimanager, and once you detect the player trys to use the inputs again use the https://docs.unity3d.com/ScriptReference/UI.Selectable.Select.html to select the reference to the last selected object. i have never done this so i am not sure if this would work, and each object having that script is the only way i can think of for getting the selected/deselected events altough feels bad for performance.
No worries!
Yes that's what I'm trying to do. The part I'm struggling with is where you said "once you detect the player trys to use the inputs again". Presumably StandaloneInput$$anonymous$$odule is still listening for these events, but how can I tap into them?
I'm writing a horrible simple hacky fix using the basic Input manager class, but it feels like there should be a more integrated way.