Hey all, question for 2021.3.x LTS and up with UI Canvas and not the new UI toolkit
In the game I’m making, there will be a lot of custom canvas elements that are interactable. In fact most if not all of the interactions are mouse click/drag on the canvas.
Underlying the canvas are art asset and logic components, but they are not to be interacted with directly by user’s input. None of them will have any collider either.
So I essentially I want to check which canvas element is clicked on. The traditional physics ray tracing doesn’t work (unless I put a collider on all of them) and I think the IPointer… interfaces are deprecated by now. What is the current best, recommended, modern approach for this?
The best way would be to make your system utilize the unity’s EventSystem. It lets you handle input of mouse, keyboard (or any other input device), while not thinking about where does the input come from. That’s how default UI elements, such as button, work, and that’s the best way to make sure your custom elements and built-in elements work together correctly (for example, so that only one of them receives the mouse click).
Sure there’s docs for EventSystem, but basically there are Raycasters (PhysicsRaycaster for 3D colliders, GraphicsRaycaster for UI elements) that need to be active, and then all you have to do is to have a raycastable component (with either collider or UI graphics) that implements some of the event handler interfaces. Take IPointerClickHandler for example.
There are many different events that unity sends, you can even make your object selectable and utilize navigation features (like when a selected button can select another butotn when you press an arrow key).
You can either implement all of these by yourself, or extend some existing built-in component for the UI. You can also implement custom graphics output (kind of like generating a mesh) to provide completely custom graphcis for the Canvas. Together with custom layout elements, this is a very powerful framework for custom UI.
Edit: IPointer… interfaces deprecated? That would be very surprising, why do you think so?