0

How can I prevent ARKit app (initially made in Unity) from placing an object on the surface when I'm clicking on the UI button on the screen?

The solution from this tutorial doesn't work:

if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) && !EventSystem.current.IsPointerOverGameObject(0))

Is there any other way to detect if I'm clicking on the UI element for IOS?

Here is the full code for placing objects using ARKit:

using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;

namespace UnityEngine.XR.iOS
{
    public class UnityARHitTestExample : MonoBehaviour
    {
        public Transform m_HitTransform;


        bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
        {
            List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
            if (hitResults.Count > 0) {
                foreach (var hitResult in hitResults) {
                    Debug.Log ("Got hit!");
                    m_HitTransform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform);
                    m_HitTransform.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform);
                    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));
                    return true;
                }
            }
            return false;
        }

        // Update is called once per frame
        void Update () {
            if (Input.touchCount > 0 && m_HitTransform != null)
            {
                var touch = Input.GetTouch(0);
                if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) && !EventSystem.current.IsPointerOverGameObject(0))
                {
                    transform.localPosition = Vector3.zero;
                    var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                    ARPoint point = new ARPoint {
                        x = screenPosition.x,
                        y = screenPosition.y
                    };

                    // prioritize reults types
                    ARHitTestResultType[] resultTypes = {
                        ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
                        // if you want to use infinite planes use this:
                        //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
                        ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, 
                        ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                    }; 

                    foreach (ARHitTestResultType resultType in resultTypes)
                    {
                        if (HitTestWithResultType (point, resultType))
                        {
                            return;
                        }
                    }
                }

                if (noShoesPlaced) {
                    UIController.GetComponent<ShoeUIController2>().showShoe1
                    noShoesPlaced = false;
                }
            }

            //if (placeFirstTime) {
            //  UIController.GetComponent<ShoeUIController> ().firstTimePlace = true;
            //  placeFirstTime = false;
        //  }

        }


    }
}
Rumata
  • 1,027
  • 3
  • 16
  • 47
  • Use the EventSystem for all your objects. – Programmer Aug 23 '17 at 23:12
  • @Programmer Thank you, how definitely should I use EventSystem? I will try the method you describe in the answer to that question. Is it also applicable for IOS? – Rumata Aug 24 '17 at 00:09
  • `EventSystem` should work on all platforms. Check the duplicate for examples depending on what type of object you want to detect clicks on. – Programmer Aug 24 '17 at 00:21
  • @Programmer Thank you, If I understood your answer correctly, in case of buttons, this way I should assign buttons to variables, and then create functions which will be called when a particular button was clicked? So this is a way to assign functions to buttons in one script, instead of manually assign it to every button in the inspector, right? But I have a lot of buttons, Is there a way to detect if I click any object of button type (or any UI element as an option)? Finger click, not the mouse click. – Rumata Aug 24 '17 at 21:06
  • @Programmer Also I wonder, why the method from the tutorial, which I posted above, doesn't work... I'm adding full ARKit code for object placing, in case if it can be helpfull. – Rumata Aug 24 '17 at 21:07
  • I just saw your updated code and I suggest you create a new question with code. Also, I suggest you forget my first comment since this is AR stuff. You can also comment under that video since the uploader seems to be responding to people. – Programmer Aug 25 '17 at 03:03

0 Answers0