How to exclude UI clicks from HitTester

  • Issue Environment : Unity Mock / On Device
  • ARDK version: 2.0
  • Unity version: 2020.3.35 LTS

Description of the issue:

I load the template AR Fundamentals - Object Placement. I only add a Canvas and a Button to it.

When I click the button, the HitTest thinks it’s an existing plane and places the virtual object at the click position.

What is the suggested way to exclude UI objects from the HitTest results?

Hi Manos,

In the Object Placement example scene, you can modify line 23 of the PlacementController.cs script to exclude touches that are over a UI element.

The Touch class contains a method named IsTouchOverUIObject() which returns a true if the touch occurred over a button, label, image, etc. in the canvas.

As published, the if-statement on line 23 appears as…

if (touch.phase == TouchPhase.Began)

…and we can modify it to appear as follows…

if (touch.phase == TouchPhase.Began && !touch.IsTouchOverUIObject())

…in order to only continue calling the enclosed TouchBegan(touch); statement if the IsTouchOverUIObject() method returns false.

3 Likes

This topic was automatically closed 2 hours after the last reply. New replies are no longer allowed.