How to hide the prefab when the augmented image is not shown in ARcore?

Hi,I was testing Augmented Image example scene .The problem I faced was I cannot hide the Prefab once it comes.When I show mu camera to Augmented Image which I set ,the prefab instantiates. Once it comes, It wont go even after I remove the image.How to stop that.I only want the prefab to come when the corresponding image is present.

namespace GoogleARCore.Examples.AugmentedImage
{
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using GoogleARCore;
    using UnityEngine;
    using UnityEngine.UI;

    /// <summary>
    /// Controller for AugmentedImage example.
    /// </summary>
    public class AugmentedImageExampleController : MonoBehaviour
    {
        /// <summary>
        /// A prefab for visualizing an AugmentedImage.
        /// </summary>
        //  public AugmentedImageVisualizer  AugmentedImageVisualizerPrefab;
        public List<AugmentVisualize> prefabs = new List<AugmentVisualize>();

        /// <summary>
        /// The overlay containing the fit to scan user guide.
        /// </summary>
        public GameObject FitToScanOverlay;

        private Dictionary<int, AugmentVisualize> m_Visualizers
            = new Dictionary<int, AugmentVisualize>();

        private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();

        /// <summary>
        /// The Unity Update method.
        /// </summary>
        public void Update()
        {
            // Exit the app when the 'back' button is pressed.
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }

            // Check that motion tracking is tracking.
            if (Session.Status != SessionStatus.Tracking)
            {
                return;
            }

            // Get updated augmented images for this frame.
            Session.GetTrackables<AugmentedImage>(m_TempAugmentedImages, TrackableQueryFilter.Updated);

            // Create visualizers and anchors for updated augmented images that are tracking and do not previously
            // have a visualizer. Remove visualizers for stopped images.
            foreach (var image in m_TempAugmentedImages)
            {
                AugmentVisualize visualizer = null;
                m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
                if (image.TrackingState == TrackingState.Tracking && visualizer == null)
                {
                    // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                    Anchor anchor = image.CreateAnchor(image.CenterPose);
                    visualizer = (AugmentVisualize)Instantiate(prefabs[image.DatabaseIndex], anchor.transform);
                    visualizer.Image = image;
                    m_Visualizers.Add(image.DatabaseIndex, visualizer);
                }
                else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                {
                    m_Visualizers.Remove(image.DatabaseIndex);
                    GameObject.Destroy(visualizer.gameObject);
                }
            }

            // Show the fit-to-scan overlay if there are no images that are Tracking.
            foreach (var visualizer in m_Visualizers.Values)
            {
                if (visualizer.Image.TrackingState == TrackingState.Tracking)
                {
                    FitToScanOverlay.SetActive(false);
                    return;
                }
            }

            FitToScanOverlay.SetActive(true);
        }
    }
}
public class AugmentVisualize : MonoBehaviour {


    public AugmentedImage Image;

    public GameObject [] Listprefabs;

    public Text Showtext;

    // Use this for initialization
    void Start ()
    {
     
    }
 
    // Update is called once per frame
    void Update ()
    {

        if (Image == null || Image.TrackingState == TrackingState.Paused)
        {
            //Listprefabs[Image.DatabaseIndex].SetActive(false);
            Listprefabs[0].SetActive(false);
            Listprefabs[1].SetActive(false);
            Showtext.text = "Paused";

            return;
        }

        else if(Image == null || Image.TrackingState == TrackingState.Stopped)
            {
            //Listprefabs[Image.DatabaseIndex].SetActive(false);
            Listprefabs[0].SetActive(false);
            Listprefabs[1].SetActive(false);
            Showtext.text = "Stopped";
            return;
        }


        else if (Image == null || Image.TrackingState != TrackingState.Tracking)
        {
            //Listprefabs[Image.DatabaseIndex].SetActive(false);
            Listprefabs[0].SetActive(false);
            Listprefabs[1].SetActive(false);
            Showtext.text = "not tracking";
            return;
        }

        else if(Image == null || Image.TrackingState == TrackingState.Tracking)
        {
            Listprefabs[Image.DatabaseIndex].SetActive(true);
            Showtext.text = "Prefab showing";
        }

    }
}
1 Like

Hello.
I have the same problem. Image tracking state never gets a value of TrackingState.Stopped.
Even running example in “AugmentedImage.scene” from “Examples” in package, image (ex. 0000/earth photo) gets the frame corners placed correctly in space and I can move the phone to look around. But when I remove image from camera view frame corners still stays on the screen.
I’m using: Unity 2018.3 and arcore-unity-sdk-v1.6.0 and running an app in Galaxy S8 with Android 8.0 and latest ARCore from Play Store.

I found this info and suggested “workaround” here.

Any solution??

Use the following code to achieve the same scenario as of TrackingState.Tracking and TrackingState.Stopped. Works fine, and tested with different images.
Its just here I’ve used AugmentedImageTrackingMethod.FullTracking and AugmentedImageTrackingMethod.LastKnownPose.

Reference is from here.

//Marker is being tracked
            if(image.TrackingState == TrackingState.Tracking && viz == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
            {
                AddVisualizer(image);
            }
            //marker is not tracked in camera output
            else if(image.TrackingMethod == AugmentedImageTrackingMethod.LastKnownPose && viz != null)
            {
                RemoveVisualizer(image, viz);
            }
1 Like