Routing interaction from Visual Element to a UI Canvas

I'm working on an application made in Unity. I've originally started with the old UI system, but a couple of weeks ago I've decided to switch to the toolkit, since it's a lot more similar to something like avalonia, that's made for apps.

9662309--1375814--upload_2024-2-24_16-13-9.jpg

I've managed to port 99% of it already and the last hurdle I ran into was the preview panel (the middle section in red). It's an image that uses a render texture from the preview camera. While working on the original version I had a problem with the UI in the preview. It's a normal canvas, however since I was using a texture, it ignored every mouse click.

9662309--1375817--upload_2024-2-24_16-26-42.jpg

I had to write my own raycaster that would change it's order priority depending if the mouse was over the preview panel.

public class PanelCameraRaycaster : GraphicRaycaster
{
    public PreviewPanelTarget targetPanel;

    public override int sortOrderPriority => targetPanel.IsFocused ? 1 : -1;

    public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
    {
        var imageSize = targetPanel.Image.rectTransform.rect.size;

        var clickPosition = eventData.position;

        RectTransformUtility.ScreenPointToLocalPointInRectangle(targetPanel.Image.rectTransform, clickPosition, null, out var pos);
        pos += imageSize / 2f;

        var oldPos = eventData.position;
        eventData.position = pos;

        base.Raycast(eventData, resultAppendList);

        eventData.position = oldPos;
    }
}

I would have to use an external script to again perform a raycast in order to figure out if the mouse was over the panel (aka. if the panel was focused).

private void CheckForFocus()
{
    var list = new List<RaycastResult>();
    EventSystem.current.RaycastAll(new PointerEventData(EventSystem.current)
    {
        position = Input.mousePosition,
    }, list);

    InFocus = targetImage.IsFocused =
        list.Where(x => !(x.module is PanelCameraRaycaster)).FirstOrDefault().gameObject == TargetImage.gameObject;
}

The reason why I couldn't just use a simple OnMouseEnter event, was that if I were to change preview raycaster's order, it would trigger the OnMouseExit event. The result of this was UI that was unresponsive half of the time, because it kept switching between two raycasters.

The problem I ran into with UI toolkit was that I was no longer able to detect if the mouse was over the preview panel and there seem to be no way of achieving that. If anyone has any idea on how to fix this, I would be glad.

Hi Dock_Frankenstein,

I'm going to start by referring you to the FAQ in the Unity Manual, where there is a question very similar to yours:
Unity - Manual: FAQ for input and event systems with UI Toolkit (unity3d.com)

If that doesn't work for you, I can help you more directly. Hope this helps!