Search Unity

UI buttons in ARcore? (helloAR example)

Discussion in 'AR' started by JasonJohn1977, Oct 11, 2017.

  1. JasonJohn1977

    JasonJohn1977

    Joined:
    Aug 12, 2013
    Posts:
    19
    Hello I've been playing with the ARcore examples and am struggling to have a working button, the helloAR example where you can place an "andy" model i've got a button hooked up to swap between different models however whenever this button is hit it also places a model is there a way to tell ARcore to ignore a certain layer / tag I'm really struggling to see it, any advice would be greatly appreciated : )
     
  2. thantieuhodo91

    thantieuhodo91

    Joined:
    Dec 3, 2014
    Posts:
    9
  3. JasonJohn1977

    JasonJohn1977

    Joined:
    Aug 12, 2013
    Posts:
    19
    HenryAsh likes this.
  4. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Hi @JasonJohn1977,

    @thantieuhodo91 is right -- the issue is that both the UI button and the HelloAR sample both respond to touch input. If you look at HelloARController.cs, around line 132, you'll see that it performs raycasts looking for a place to put the andyObject anytime there is a touch input. This is a good starting point, but to extend it to support the type of logic you're describing, you may want to just copy this code into your own component and only perform the hit test on ARCore planes when the a button is not being pressed. So basically wrap that raycast logic in a

    Code (csharp):
    1. if (!IsPointerOverUIObject()) {...}
    type of statement.

    Cheers,
    Tim
     
  5. JasonJohn1977

    JasonJohn1977

    Joined:
    Aug 12, 2013
    Posts:
    19
    Hi @timmunity

    This fixed my issue, I used the line
    Code (CSharp):
    1. if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId)) {...}
    thanks so much for your help : )
     
  6. jarod-smith

    jarod-smith

    Joined:
    Feb 15, 2018
    Posts:
    1
    Hi, I seem to be having the same issue when adding @JasonJohn1977 's code the prefab simply won't even place. what do you think could be going wrong? For reference here is my code snippet,

    Code (CSharp):
    1.  
    2. if (Session.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit) && doPlace && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
    3.             {
    4.                 var andyObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
    5.  
    6.                 // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
    7.                 // world evolves.
    8.                 var anchor = hit.Trackable.CreateAnchor(hit.Pose);
    9.  
    10.                 // Andy should look at the camera but still be flush with the plane.
    11.                 andyObject.transform.LookAt(FirstPersonCamera.transform);
    12.                 andyObject.transform.rotation = Quaternion.Euler(0.0f,
    13.                     andyObject.transform.rotation.eulerAngles.y, andyObject.transform.rotation.z);
    14.  
    15.                 // Make Andy model a child of the anchor.
    16.                 andyObject.transform.parent = anchor.transform;
    17.                 doPlace = false;
    18.             }
    19.  
     
  7. aakashdabrase

    aakashdabrase

    Joined:
    Aug 31, 2017
    Posts:
    3
    Don't add the condition in above if statement using &&. Just wrap raycast using if(!EventSystem.current.IsPointerOverGameObject(touch.fingerId)){...}
    Also check your doPlace var, maybe its always false
     
    Last edited: Feb 16, 2018
  8. tinatotty91

    tinatotty91

    Joined:
    Jul 26, 2018
    Posts:
    3
    Hi, sorry for the dumb question but I'm a complete beginner. I'm trying to create a item menu for a AR scene in Unity. In my scene you can choose between different 3D items in a list and than spawn the corrisponding item when I touch a tracked plane in the app. When I try to spawn a single tipe of 3D object it works (basically like helloAR), but I can't figure out how to make it work with a list of objects.
    Thanks!
     
  9. danigidiu

    danigidiu

    Joined:
    Jul 31, 2018
    Posts:
    2
    Hi,My problem is similar I want to create a button with a list of objects from where to select different objects and put them in the real world. Can someone help with a script or instructions to do that?
     
  10. danigidiu

    danigidiu

    Joined:
    Jul 31, 2018
    Posts:
    2
    @jarod-smith Do I get error with Raycast and doplace?Can you send me all your code?
     
  11. yanwenwen813283348

    yanwenwen813283348

    Joined:
    Feb 19, 2019
    Posts:
    1
    hallo I have the same question with you, I would like to add a UI bottom as a menu to select the item. I wonder whether your problem has been solved or not, and how did you deal with it?
     
  12. tinatotty91

    tinatotty91

    Joined:
    Jul 26, 2018
    Posts:
    3
    I recommend you to check out this Udemy course: https://www.udemy.com/create-ar-placement-app-and-full-template-for-photo-app/
    I found it extremely interesting and, since I am a beginner, it put me on track to develop more interesting ar apps. It also explains how to create a menu for your ar app and gives you a working solution on wich you can build further.
    The teacher Satwant Singh also has an interesting youtube channel where he uploads more videos on ar
     
    ROBYER1 likes this.
  13. robrtoprz

    robrtoprz

    Joined:
    May 15, 2017
    Posts:
    9
    So, the same question, I tried to implement it in ARCore v1.13.0 and I changed the line:
    Code (CSharp):
    1. if (!EventSystem.current.IsPointerOverGameObject(gesture.FingerId))
    2.  
    because touch.fingerId changed for gesture.FingerId, but this doesn't work properly, am I missing something?

    Code (CSharp):
    1. namespace GoogleARCore.Examples.ObjectManipulation
    2. {
    3.     using GoogleARCore;
    4.     using UnityEngine;
    5.     using UnityEngine.EventSystems;
    6.  
    7.     /// <summary>
    8.     /// Controls the placement of objects via a tap gesture.
    9.     /// </summary>
    10.     public class PawnManipulator : Manipulator
    11.     {
    12.         /// <summary>
    13.         /// The first-person camera being used to render the passthrough camera image (i.e. AR
    14.         /// background).
    15.         /// </summary>
    16.         public Camera FirstPersonCamera;
    17.  
    18.         /// <summary>
    19.         /// A prefab to place when a raycast from a user touch hits a plane.
    20.         /// </summary>
    21.         public GameObject PawnPrefab;
    22.  
    23.         /// <summary>
    24.         /// Manipulator prefab to attach placed objects to.
    25.         /// </summary>
    26.         public GameObject ManipulatorPrefab;
    27.  
    28.         /// <summary>
    29.         /// Returns true if the manipulation can be started for the given gesture.
    30.         /// </summary>
    31.         /// <param name="gesture">The current gesture.</param>
    32.         /// <returns>True if the manipulation can be started.</returns>
    33.         protected override bool CanStartManipulationForGesture(TapGesture gesture)
    34.         {
    35.             if (gesture.TargetObject == null)
    36.             {
    37.                 return true;
    38.             }
    39.  
    40.             return false;
    41.         }
    42.  
    43.         /// <summary>
    44.         /// Function called when the manipulation is ended.
    45.         /// </summary>
    46.         /// <param name="gesture">The current gesture.</param>
    47.         protected override void OnEndManipulation(TapGesture gesture)
    48.         {
    49.             if (gesture.WasCancelled)
    50.             {
    51.                 return;
    52.             }
    53.  
    54.             // If gesture is targeting an existing object we are done.
    55.             if (gesture.TargetObject != null)
    56.             {
    57.                 return;
    58.             }
    59.  
    60.             // Raycast against the location the player touched to search for planes.
    61.             TrackableHit hit;
    62.             TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon;
    63.  
    64.             if (!EventSystem.current.IsPointerOverGameObject(gesture.FingerId))
    65.             {
    66.                 if (Frame.Raycast(
    67.                 gesture.StartPosition.x, gesture.StartPosition.y, raycastFilter, out hit))
    68.                 {
    69.                     // Use hit pose and camera pose to check if hittest is from the
    70.                     // back of the plane, if it is, no need to create the anchor.
    71.                     if ((hit.Trackable is DetectedPlane) &&
    72.                         Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
    73.                             hit.Pose.rotation * Vector3.up) < 0)
    74.                     {
    75.                         Debug.Log("Hit at back of the current DetectedPlane");
    76.                     }
    77.                     else
    78.                     {
    79.                         Debug.Log("PLANE DETECTED");
    80.                         // Instantiate game object at the hit pose.
    81.                         var gameObject = Instantiate(PawnPrefab, hit.Pose.position, hit.Pose.rotation);
    82.  
    83.                         // Instantiate manipulator.
    84.                         var manipulator =
    85.                             Instantiate(ManipulatorPrefab, hit.Pose.position, hit.Pose.rotation);
    86.  
    87.                         // Make game object a child of the manipulator.
    88.                         gameObject.transform.parent = manipulator.transform;
    89.  
    90.                         // Create an anchor to allow ARCore to track the hitpoint as understanding of
    91.                         // the physical world evolves.
    92.                         var anchor = hit.Trackable.CreateAnchor(hit.Pose);
    93.  
    94.                         // Make manipulator a child of the anchor.
    95.                         manipulator.transform.parent = anchor.transform;
    96.  
    97.                         // Select the placed object.
    98.                         manipulator.GetComponent<Manipulator>().Select();
    99.                     }
    100.                 }
    101.             }
    102.         }
    103.     }
    104. }