gameobject's onmouseover conflicting with UI ipointerclick handler

Hello,
I have a UI obejct and 3d gameobject that are fighting over a mouse click. The 3d Gameobject is behind the UI button. I want a UI button (onvaluechanged listener) to take precedence over the gameobjects onmouseover event.
Is there a way to get the method attached to the listener to run before the onmouseover event ? the only other solution (i can think of) would be to ignore the click by checking if the mouse pointer was out-of-bounds on open UI windows, then ignoring it, but that is not preferred since that will take up precious cpu time. I am looking for a more optimized way. I am currently using 2018.2.

Wat… how precious is your CPU time that you cannot compare a few integers!!!

I would be far more worried about how to make an elegant solution that you can debug into the future when you change how other stuff works and forget details of this stuff.

To that end it might be best to make your UI soak up ALL the touches, such as with a full screen invisible button underneath to catch anything that misses your real buttons, then re-cast the mouse into the scene to hit the 3D stuff. It’s just so trivial to make your own OnMouseOver type event system, it’s hardly worth sticking with the in-built one.

@Kurt-Dekker , thank you for the contribution
My game would have arbitrarily 15 open windows with window Rect’s that I would need to compare the mouse pointer location with. I would also have to convert the 3d gameobject transform back into UI screen coordinates for the comparison.
if there are zero UI windows in bounds of the mouse click, then the 3d gameobject is a valid click. a catchall panel with raycasting the mouse click back into the scene seems like it would be adding back some cpu time that I would be trying to save.
With all the extra math, it seems like I could be introducing errors.

My idea for an elegant solution would be to set button click events on the UI, to have higher precedence than clicks on gameObjects. Stacking UI elements within the canvas already solves with sibling index.

AFAIK, unity has not implemented this kind of ordering in the engine.

Are there any other ideas ?

@LethalInjection
“if there are zero UI windows in bounds of the mouse click, then the 3d gameobject is a valid click”

If you need to check if your cursor is / is not over UI object there is this:
EventSystem.current.IsPointerOverGameObject()

Note in this case, GameObject == UI object.

I think modern multi-megahertz CPUs are quite capable of comparing 15 or even 1500 x 4 integers.

Who knows. If they did they would the exact same CPU cycles to do it.

The only way to know for sure is to look in the profiler (Window → Analyze → Profiler)… if you do NOT see a problem there, you are wasting time optimizing for a problem that doesn’t exist.

I would disagree and native ordering would (in theory) be faster, since onmouseover currently happens before UI eventsystems ipointer clicks. Reversing this in the engine would mean no need for me to do any comparisons.

I don’t need to bother looking at the profiler since adding the for loop to check all windows versus not doing it all and just having the engine check for UI button ipointer clicks from eventsystems first, seems obviously faster.

Simply put, the engine currently does A, then B. I am saying just do B then A. No added code needed on my part.
What I think you are suggesting is do C, A, then B, when C is the added code for checking windows.

@eses
Thanks for the contribution.
“EventSystem.current.IsPointerOverGameObject()”
That call is already included and executes properly, but parent onmouseover happens before the call to UI button listeners, I want onmouseover events to happen after ipointerclick . This will give UI objects higher precedence than world gameObjects.

Where is the message/event system in the execution order ?

@LethalInjection

“That call is already included and executes properly, but parent onmouseover happens before the call to UI button listeners, I want onmouseover events to happen after ipointerclick”

Isn’t OnMouseOver part of MonoBehaviour class itself?

UI elements and EventSystem are something that you add to your scene. And IIRC UI elements always react after these old click events you get from MonoBehaviour. It doesn’t matter even if you change Input Actions Per Second of Standalone Input Module, Debug.Log of button clicks gets printed later than OnMouseDown or OnMouseUp. And there doesn’t seem to be a way to somehow use script execution order either.

So I’ve personally done what @Kurt-Dekker said:
“…make your UI soak up ALL the touches, such as with a full screen invisible button underneath to catch anything that misses your real buttons, then re-cast the mouse into the scene to hit the 3D stuff.”

And I have read several similar answers (like this one):

So you could simply forget the old MonoBehaviour OnMouseDown and OnMouseUp and use the new EventSystem only. I don’t think one raycast would kill your game performance…