Hit Testing Not Working after 3.1.0 Upgrade
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
}
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
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
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
}
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
}
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Hit Testing Not Working after 3.1.0 Upgrade
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:
We'll fix the documentation, thanks for pointing this out.
Code: Select all
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
}
}
}
Re: Hit Testing Not Working after 3.1.0 Upgrade
That works great, thanks!