What determines the shape of the hover effect for canvas elements?

For canvas UI in Polyspatial, is there a way to change the shape of the hover effect? I’m only seeing rectangular hover effects. In particular, we are procedurally setting the vertices of a MaskableGraphic to create a capsule shape for a button but the hover effect ends up being rectangular

It should reflect the geometry of the Graphic (Image, e.g.). I’ll try to verify that that’s the case and provide an example.

This was just a quick test of modifying the Mesh of a Button:

using System.Collections;
using System.Linq;
using UnityEngine;

public class MeshChanger : MonoBehaviour
{
    IEnumerator Start()
    {
        var canvasRenderer = GetComponent<CanvasRenderer>();

        Mesh originalMesh;
        do {
            yield return null;
            originalMesh = canvasRenderer.GetMesh();
        } while (originalMesh == null);

        var newMesh = Object.Instantiate(originalMesh);

        newMesh.vertices = originalMesh.vertices.Select(v => new Vector3(v.x, v.y + v.x, v.z)).ToArray();

        canvasRenderer.SetMesh(newMesh);
    }
}

Basically, the VisionOS Hover Effect (which is to say, the RealityKit HoverEffectComponent) just modifies the material of the corresponding Renderer (CanvasRenderer/MeshRenderer/SpriteRenderer).

If you’re unable to get that to work, though, please submit a bug report and let us know the incident number (IN-#####) so that we can look into it.

Ok, thanks for the info. that makes things a little more clear about how the hover effect works. I tested your use case which works as expected. After some further testing, it looks like I’m not actually getting a hover effect at all in my case. As a simple test, I put RawImage and VisionOSHoverEffect components on a GameObject. I get hover effects. Then I created a new component, copied the source code from RawImage into the new component and renamed it HoverImageTest. Replaced the RawImage component with HoverImageTest. Now I don’t get hover effects. I’m imagining that should work? I can file a bug if so.

I think the missing piece here is the Collider. From the documentation:

Three components must be present to achieve the hover effect:

A VisionOSHoverEffect component indicates that the GameObject and its descendants should display an effect when hovered.
A Collider component on the same GameObject as the VisionOSHoverEffect defines the collision shape against which the gaze ray is cast.
A MeshRenderer provides the mesh and geometry on which the coloration affect is applied. The MeshRenderer may be on the GameObject containing the VisionOSHoverEffect and Collider components or on any of its descendants.

For Image and RawImage, as a special case, we create colliders automatically based on the image geometry. If you don’t use those exact classes, though, you won’t get the automatic collider, and will have to add a Collider explicitly to the GameObject. It can simply be a trigger collider. That should make the hover effect work. The docs say you need a MeshRenderer, but a CanvasRenderer should also work.

Ahh! ok. Adding the collider works with my test case. The thing that was confusing the diagnosis is that the hover effect seems to also include the materials of the children of the GameObject that do not have a VisionOSHoverEffect. Is there any way to prevent that? On the button we’re using there’s a front plate that’s used for near interactions that we don’t want to get the hover effect. We would just want the back plate to get the hover effect. For now I guess I could modify the geometry of the RawImage on the front plate to match the backplate.

Unfortunately, no; that’s basically just how Apple’s HoverEffectComponent works (it applies to all descendants of the entity to which it’s applied, as well as the entity itself).

Alright thanks for the help. I rearranged the hierarchy a bit and have something working now

1 Like

Kapolka, does apple change the colour of the ui image for the hover effect, in which case can unity tell if the colour has been changed. I wanted to make a scale up on hover by checking the colour change but it does not seem to be the case.

No. There is no way to tell when the hover effect is active. It is an OS level effect that is handled by RealityKit and Unity / your app have no knowledge of.

1 Like