alfiare
Topic Author
Posts: 16
Joined: 30 Nov 2017, 23:47

Hit Testing Not Working after 3.1.0 Upgrade

20 Aug 2021, 04:29

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
}

Tags:
 
alfiare
Topic Author
Posts: 16
Joined: 30 Nov 2017, 23:47

Re: Hit Testing Not Working after 3.1.0 Upgrade

20 Aug 2021, 04:42

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.
 
alfiare
Topic Author
Posts: 16
Joined: 30 Nov 2017, 23:47

Re: Hit Testing Not Working after 3.1.0 Upgrade

20 Aug 2021, 05:01

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
}
 
User avatar
sfernandez
Site Admin
Posts: 2415
Joined: 22 Dec 2011, 19:20

Re: Hit Testing Not Working after 3.1.0 Upgrade

20 Aug 2021, 12:20

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.
 
alfiare
Topic Author
Posts: 16
Joined: 30 Nov 2017, 23:47

Re: Hit Testing Not Working after 3.1.0 Upgrade

20 Aug 2021, 15:54

That works great, thanks!

Who is online

Users browsing this forum: No registered users and 2 guests