How do I create clickable and highlightable buttons in Unity?

0 votes
asked Jun 20 by Chief (16 points)
edited Jun 20 by Chief
I have created a canvas and aligned it to the viewport and have added UI buttons that I need to respond to the stylus by highlighting when hovered over or clicked.

I have enabled Mouse Emulation in the inspector.

They do not respond to the stylus at all currently.

I am using Unity 2017 with C# and the StylusObjectManiupulationSample script for grabbing and rotating objects.

1 Answer

0 votes
answered Jun 22 by AlexShorey (200 points)
Hi Chief,

The problem with mouse interaction you're probably encountering is the UI canvas referencing the wrong camera to determine where the mouse is. When zCore initializes, it creates and draws from its own internally managed cameras not present in the scene inspector.

My best advice for that particular issue is to give your canvas a reference to zCore's center camera on start.

canvas.worldCamera = zCore.GetCenterCamera();

UI+Stylus is a different matter. Currently, we're leaving that up to developers to formulate their own solutions. The latest, and best approach I've seen involves extending Unity's "StandardInputModule". I'm looking into the possibility of us sharing one of those potential solutions and will report back here in that event that I get a green light. For now I'd recommend working under the assumption that you'll have to kick up something from scratch.

Alex S.
commented Jul 5 by AlexShorey (200 points)
edited Jul 5 by AlexShorey

zCore.GetCenterCamera(); isn't an available function in the current 5.0 release of zcore.  The alternative is a bit more complicated, so I'm linking a script here that will facilitate a proper UI raycast camera.

Here are some instructions to get it working.
- Add an extra camera to the scene.
- Disable/uncheck the camera component. (since it doesn't need to draw anything)
- Add the script linked below to the  gameobject of the new camera.
- Assign the new camera to the "Event Camera" property of the UI's canvas  component through the inspector.

(When testing in the editor, resize zCore's preview window to match the position and dimensions of the game view window.)

I'm continuing to look into the possibility of sharing more UI solutions. Stay tuned.

Alex S.