I’ve noticed that for any script using On Pointer Enter Handler, the on enter event is triggered twice when a children graphic blocks raycast. E.g I have a children image that is within a parent image with the pointer handler script. both image blocks raycast. On entering the parent image, pointer enter event is triggered, On entering the children image, On Enter is triggering again.
This issue extends out to buttons where it goes from Highlighted → Normal → Highlighted state.
Any idea how to prevent this issue from happening? (other than disabling raycast for all children objects)
I was wondering if there was a more elegant way of resolving this issue.
Disabling raycast on children graphics doesn’t cause an issue currently with my current projects, but was just trying to account for possible future use cases where we want the children to block raycast.
What version of unity are you on? There was a range of versions ~2021.3.x-2021.3.6 and probably 2022 series as well where Unity tried to change how pointer enter/exit events work for child elements. Don’t remember precisely the first one where it started, but i don’t think it was present in first few of 201.3.x. More or less you would get additional mouse enter/exit events when transitioning between parent and child with some flags set. But after complains from a lot of people that the change breaks their UI (including firsty components created by Unity) the default behavior was changed back to what it was before. You can still get the new behavior by setting an option (although not sure if anyone wants that).
I think that since that change that Unity introduced, you can tell if the event is reentering a children, or completely leaving your whole graphics hierarchy with the data that PointerEventData paramter gives you.
public void OnPointerExit(PointerEventData eventData) {
eventData.fullyExited
/// Specifies in the case of a pointer exit if the pointer has fully exited the area or if it has just entered a child.
eventData.reentered
/// Specifies in the case of a pointer enter if the pointer has entered a new area or if it has just reentered a parent after leaving a child.
The behavior was reverted back to old one in 2021.3.6 (I should have said ~2021.3.x-2021.3.5 in my previous post) . So if you are on 2021.3.10 what you are seeing is something else (unless you accidentaly changed the setting which activates the new behavior).
Change log for 2021.3.6 contains:
The versions mentioned in issues are a bit confusing since because either there was a regression after they fixed it first time or the “fix” broke stuff for a lot of projects, hence reverting default behavior to what it was before.
@iMobCoding How is that related to pointer events. Isn’t that only for move/submit/cancel events? Are you sure you didn’t mean this?
By the way name 'sendPointerHoverToParent" is somewhat confusing. Since depending on whether child object also has pointer event handler, disabling it can cause amount of pointer events parent receives to increase not decrease.
If you’re working with Unity Remote, testing on a connected device through Unity Remote, and If none of the above helps you, make sure to check the pointerID of the event and make sure it’s not -1 (left mouse button).
For some reason, when operating through Unity Remote, there will be a random “left mouse click” (that won’t happen on a release build) when you touch the screen. Not sure where the issue comes from (Unity Remote or Unity itself), but it’s something to test for if you’re getting a single extra OnPointer event.