Page 1 of 1

Hit Testing Not Working after 3.1.0 Upgrade

Posted: 20 Aug 2021, 04:29
by alfiare
I recently upgraded to 3.1.0 I noticed that the hit testing code below (taken from the Unity doc) seems to not work anymore. It finds hits where there's nothing and over actual buttons and things it doesn't find a hit. I also noticed that it seems like all the UI elements got larger. Anyone have any ideas what I did wrong?


UnityEngine.Vector3 mousePos = Input.mousePosition;
Point point = new Point(mousePos.x, Screen.height - mousePos.y);
HitTestResult hit = VisualTreeHelper.HitTest(this._root, point);
if (hit.VisualHit == null)
{
// No UI element was hit

// ...your code here
}

Re: Hit Testing Not Working after 3.1.0 Upgrade

Posted: 20 Aug 2021, 04:42
by alfiare
I've also found that if I compare Unity's Screen.width/height vs VisualTreeHelper.GetSize((noesis view from camera).Content) they are different, the VisualTreeHelper.GetSize is quite a bit smaller.

Re: Hit Testing Not Working after 3.1.0 Upgrade

Posted: 20 Aug 2021, 05:01
by alfiare
Also found that if I manually adjust the point location based on the ratio of Unity's Screen to Noesis's size then I seem to get accurate hit testing, the following works

Visual root = (Visual)(NoesisView from camera).Content;
UnityEngine.Vector3 mousePos = Input.mousePosition;
Point point = new Point(mousePos.x, Screen.height - mousePos.y);
var noesisSize = VisualTreeHelper.GetSize(root);
point.X = (noesisSize.Width / Screen.width) * point.X;
point.Y = (noesisSize.Height / Screen.height) * point.Y;

HitTestResult hit = VisualTreeHelper.HitTest(root, point);
if (hit.VisualHit == null)
{
// No UI element was hit

// ...your code here
}

Re: Hit Testing Not Working after 3.1.0 Upgrade

Posted: 20 Aug 2021, 12:20
by sfernandez
In NoesisGUI 3.1 we implemented support for HDPI screens so UI gets automatically scaled, but the documentation regarding HitTest was unfortunately not updated accordingly. The provided example code should look like this:
using UnityEngine;
using Noesis;

public class HitTestSample : MonoBehaviour
{
    Visual _root;

    void Start()
    {
        NoesisView view = GetComponent<NoesisView>();
        _root = (Visual)VisualTreeHelper.GetRoot(view.Content);
    }

    void OnMouseDown()
    {
        Vector3 mousePos = Input.mousePosition;
        Point point = _root.PointFromScreen(new Point(mousePos.x, Screen.height - mousePos.y));
        HitTestResult hit = VisualTreeHelper.HitTest(this._root, point);
        if (hit.VisualHit == null)
        {
            // No UI element was hit

            // ...your code here
        }
    }
}
We'll fix the documentation, thanks for pointing this out.

Re: Hit Testing Not Working after 3.1.0 Upgrade

Posted: 20 Aug 2021, 15:54
by alfiare
That works great, thanks!