Search Unity

Question ARFoundation ARKit clear out object before recognizing new image targets

Discussion in 'AR' started by markpanning, Dec 4, 2020.

  1. markpanning

    markpanning

    Joined:
    May 19, 2017
    Posts:
    1
    Hello,
    I am trying to write my first iOS app using ARFoundation and ARKit to do Image Tracking and starting very simple. Starting by following several tutorials that seem to do image tracking on iOS using ARFoundation and ARKit.
    My simple program builds to iPhone successfully and when it detects the image it displays a sphere over the image.

    However, After adding additional images to my Reference Image Library, I end up with an additional sphere over the image target each time I recognize a new image target.

    How can I get rid of the sphere from the previous image target before recognizing a new image target?

    I am using a MacBook Pro running Catalina, Xcode 12.2, Unity 2019.2.13f1, ARFoundation 2.2.0 preview 3, ARKit XR plugin 2.2.0 preview 3 AR Subsystem 2.2.0 preview 3

    Below is the script I am following and thinking I can clear it out in the OnImageChanged somehow?

    Thank you for any help you can provide.

    Thank you,
    Mark

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using UnityEngine.XR.ARFoundation;
    6.  
    7. public class ImageRecognitionExample : MonoBehaviour
    8. {
    9.     private ARTrackedImageManager _aRTrackedImageManager;
    10.  
    11.  
    12.     private void Awake()
    13.     {
    14.         Debug.Log("in awake");
    15.         _aRTrackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    16.     }
    17.  
    18.     public void OnEnable()
    19.     {
    20.         Debug.Log("in OnEnable");
    21.         _aRTrackedImageManager.trackedImagesChanged += OnImageChanged;
    22.     }
    23.     public void OnDisable()
    24.     {
    25.         Debug.Log("in OnDisable");
    26.         _aRTrackedImageManager.trackedImagesChanged -= OnImageChanged;
    27.     }
    28.  
    29.     public void OnImageChanged(ARTrackedImagesChangedEventArgs args)
    30.     {
    31.         Debug.Log("in onImageChanged");
    32.         foreach (var trackedImage in args.added)
    33.         {
    34.             Debug.Log(trackedImage.name);
    35.         }
    36.     }
    37. }