How do I create clickable and highlightable buttons in Unity?

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.

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.

Apologies,

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 sharing 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.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using zSpace.Core;

public class uiRaycastCamAlign : MonoBehaviour {
    private Camera cam;
    private ZCore core;
    private ZCore.Pose pose;

    private Transform main_cam_trans;

    private static readonly Matrix4x4 s_flipHandednessMap = Matrix4x4.Scale(new Vector4(1.0f, 1.0f, -1.0f));

    void Start () {
        core = GameObject.FindObjectOfType<ZCore>();
        cam  = gameObject.GetComponent<Camera>();
        main_cam_trans = core.CurrentCameraObject.transform;
    }

    void Update () {
        AlignCam();
    }
    private void AlignCam(){
        ZCore.Eye eye = ZCore.Eye.Center;
        cam.projectionMatrix  = core.GetFrustumProjectionMatrix(eye);

        transform.position = core.GetFrustumEyePosition(eye, ZCore.CoordinateSpace.World);

        Matrix4x4 v_mtx = FlipHandedness(core.GetFrustumViewMatrix(eye));
        Matrix4x4 cam_mtx = main_cam_trans.localToWorldMatrix * v_mtx.inverse;
        transform.position = cam_mtx.GetColumn(3);
        transform.rotation =
                Quaternion.LookRotation(
                cam_mtx.GetColumn(2),
                cam_mtx.GetColumn(1));
    }

    private Matrix4x4 FlipHandedness(Matrix4x4 matrix) {
        return s_flipHandednessMap * matrix * s_flipHandednessMap;
    }
}

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

Alex S.

Hi,

I also can’t get the stylus to work on Unity UI elements, and this script does not work for me. I am using Unity 2018.2 with StylusObjectManipulationSample.cs and have enabled mouse emulation.

Hi there,

The solution above is for the purpose of enabling interaction by mouse input. It only works for the stylus so long as mouse emulation is enabled on the zCore instance.

Besides mouse emulation, out-of-the-box stylus UI interaction isn’t available yet. The best solution at the moment is to write a custom version of Unity’s StandardInputModule to work with the stylus as an input device.

As soon as a pre-made solution does become available, I will be broadcasting that information to relevant posts on this forum, and to registered developer emails.