Search Unity

Blocking raycasts from passing through UI?

Discussion in 'UGUI & TextMesh Pro' started by sampletape8, Jun 6, 2019.

  1. sampletape8

    sampletape8

    Joined:
    Sep 7, 2018
    Posts:
    6
    Hey guys,

    so I read a lot about this topic on the internet, but nothing seemed to work for me so far, so here is my problem: I have a ARKit scene in my project, which lets me place objects when clicking on the floor. On the bottom of my screen, there is a ScrollRect with different buttons, which replace the augmented model. But everytime I click on the buttons, I also click on the "ARKit plane layer" underneath it, which I don't want to happen.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine.EventSystems;
    4.  
    5. namespace UnityEngine.XR.iOS
    6. {
    7.     public class UnityARHitTestExample : MonoBehaviour
    8.     {
    9.         public Transform m_HitTransform;
    10.         public float maxRayDistance = 30.0f;
    11.         public LayerMask collisionLayer = 1 << 10;  //ARKitPlane layer
    12.  
    13.         bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
    14.         {
    15.             List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);
    16.             if (hitResults.Count > 0)
    17.             {
    18.                 foreach (var hitResult in hitResults)
    19.                 {
    20.                     Debug.Log("Got hit!");
    21.                     m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
    22.                     m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
    23.                     Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
    24.                     return true;
    25.                 }
    26.             }
    27.             return false;
    28.         }
    29.  
    30.         // Update is called once per frame
    31.         void Update()
    32.         {
    33. #if UNITY_EDITOR   //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
    34.             if (Input.GetMouseButtonDown(0))
    35.             {
    36.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    37.                 RaycastHit hit;
    38.  
    39.                 //we'll try to hit one of the plane collider gameobjects that were generated by the plugin
    40.                 //effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
    41.                 if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
    42.                 {
    43.                     //we're going to get the position from the contact point
    44.                     m_HitTransform.position = hit.point;
    45.                     Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
    46.  
    47.                     //and the rotation from the transform of the plane collider
    48.                     m_HitTransform.rotation = hit.transform.rotation;
    49.                 }
    50.             }
    51. #else
    52.             if (Input.touchCount > 0 && m_HitTransform != null)
    53.             {
    54.                 var touch = Input.GetTouch(0);
    55.                 if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
    56.                 {
    57.                     var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
    58.                     ARPoint point = new ARPoint {
    59.                         x = screenPosition.x,
    60.                         y = screenPosition.y
    61.                     };
    62.  
    63.                     // prioritize reults types
    64.                     ARHitTestResultType[] resultTypes = {
    65.                         //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry,
    66.                         ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
    67.                         // if you want to use infinite planes use this:
    68.                         //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
    69.                         //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
    70.                         //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
    71.                         //ARHitTestResultType.ARHitTestResultTypeFeaturePoint
    72.                     };
    73.                  
    74.                     foreach (ARHitTestResultType resultType in resultTypes)
    75.                     {
    76.                         if (HitTestWithResultType (point, resultType))
    77.                         {
    78.                             return;
    79.                         }
    80.                     }
    81.                 }
    82.             }
    83. #endif
    84.  
    85.         }
    86.     }
    87. }
    88.  
    89.  
    How can I achieve the UI from blocking the raycasts? Is there an easier way to do this then changing the whole code? Can I use Graphic Raycaster component on my canvas, if so, how? I already tried but nothing changed when clicking on my buttons.