Is there some reason why event scripts like OnMouseDown etc don’t fire on UI Canvas objects?
The reason I’m asking is that I want to trap when the user clicks and drags on an empty area of the Canvas.
thanks
Is there some reason why event scripts like OnMouseDown etc don’t fire on UI Canvas objects?
The reason I’m asking is that I want to trap when the user clicks and drags on an empty area of the Canvas.
thanks
Does your objects have a collider? According to the documentation OnMouseDown requires one. Though I’m not sure if UI elements are excluded even if they had colliders.
You could also create a graphic behind everything else with “raycastTarget” enabled. That object would also have your own component attached, which would implement IPointerDownHandler (and other related interfaces). Then input not handled by other elements should reach this component.
Simply put, the UI system does not use OnMouseDown or any of the other inherited MonoBehaviour events.
You need to use the EventSystem events from a UI object to receive input from the event system on the object you want to target. Sorry if that sounds confusing.
Either look how the UI objects in the UI Extensions project use events like this (link in sig) or watch the UI Learn tutorials to get more info.
ok thanks, I was experimenting with a few different methods when I posted that question. At one point, I created a script that implemented the IPointerDownHandler interface and attached it to Canvas, but the event only fired when the mouse was over its child UI Objects.
I’ll look at the UI Extensions project, hopefully I can create an invisible panel that fills the entire screen and traps mouse events, much like a traditional windows form.
Yeah, I thought about using a graphic in that manner, but I didn’t want to go down that route if there was a more elegant / correct solution.
At the moment, I’m using a script with OnGUI() and Update() to accomplish what I need, but I’d rather just move all that code to a UI Object.
There is the NonDrawingGraphic control which works like that and it also doesn’t draw
allowing you to grab events freely. (check the latest update video on the YouTube Channel to see that)
But what is your use case? If you are wanting to just capture mouse events, why use the UI system, unless you want it to interact with the UI.
Or are you trying to make a region selector? (p.s. then also check the RTSSelector control which can select both UI and 3D objects
)