UI events and child gameobjects?

Hi,

I’ve been building experimental UI click / drag’n’drop handler script that would be would located in only one script… I’d like to handle clicks, pointer over and drag’n’drop events in one script… for both gameobjects and UI objects (Maybe it’s a bad idea, but anyhow).

However when using Unity UI events like IPointerEnter and so on, I’ve noticed that they don’t seem to work in all cases, if script doing the event checking is in parent object, like this:

Parent (ClickCheckerComponentIMade)
---- Child UI element A
---- Child UI element B

Now, if I move pointer over element A, I’ll get PointerEnter in Parent, but if A and B are overlapping, and I move pointer from A to B, no PointerEvent gets registered. However, IIRC by having a IPointerEnter and Exit implemented in a script which exists in element A and element B, will work properly…

Also, PointerEvent data provided by the event system seems to be really vague - For example, why do we get Canvas as gameobject on PointerDown event, instead of element A or B, if I clicked element A or B?

There seems to be many gameObjects in provided PointerEventData, but I didn’t find anything but pointerEnter to be useful so far…

Questions:

  1. Is it a bad idea to try to use UI events so that event methods are in one “manager” script instead of being located in slots / panels themselves?

  2. Are there any good sources that would explain PointerEvent data? I know inspector shows results of clicks… but…

OK - did a little bit more testing. Pilot errors.

Probably had just forgotten it, but basically I messed up the concepts while playing with PointerEventData.

Having script with IPointerEnter and IPointerExit on Canvas object, will actually always detect clicks that hit its children as hits to gameObject that has the script (canvas in this case).

Also, now that I did more testing, PointerEventData’s “pointerEnter” seems to show correct data, even if the script was in canvas gameObject, it shows in pointerEnter correct objects (element A and B) when changing from overlapping UI element to other.

EDIT: but even if this data seems to change in PointerEventData, it doesn’t mean that IPointerEnter method gets fired, when moving from element A to B, which sort of now forms one blob, that spits out Enter/Exit only when their common blob border is crossed… this is probably because system wasn’t meant be used like this…

EDIT: I’ll test this a bit more, but seems like I’ll be using raycasts and GraphicRaycaster without EventSystem, as I got everything sort of working…

1 Like