Search Unity

How to show different prefabs for different images in ARcore-Augmented Image scene using Unity?

Discussion in 'AR' started by zyonneo, Jun 18, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I am trying to augment different prefabs for different images say around 20 models.Currently testing with 2 models for 2 images in AugmentedImage sample scene.

    I am making an array of AugmentedImageVisualizer
    Code (CSharp):
    1. public AugmentedImageVisualizer[] AugmentedImageVisualizerPrefab;
    I have made 4 prefabs.To each prefab I am adding the Augmented Image Visualizer (script).

    Code (CSharp):
    1.  public class AugmentedImageVisualizer : MonoBehaviour
    2.     {
    3.         /// <summary>
    4.         /// The AugmentedImage to visualize.
    5.         /// </summary>
    6.           public AugmentedImage Image;
    7.  
    8.      
    9.  
    10.         public GameObject prefabToInstantiate;
    11.  
    12.         /// <summary>
    13.         /// The Unity Update method.
    14.         /// </summary>
    15.         public void Update()
    16.         {
    17.             if (Image == null || Image.TrackingState != TrackingState.Tracking)
    18.             {
    19.              
    20.                 prefabToInstantiate.SetActive(false);
    21.  
    22.                 return;
    23.             }
    24.  
    25.  
    26.             prefabToInstantiate.SetActive(true);
    27.  
    28.         }
    29.     }
    30. }
    The AugmenetedImageExampleController script is given below...



    Code (CSharp):
    1. public class AugmentedImageExampleController : MonoBehaviour
    2.     {
    3.         /// <summary>
    4.         /// A prefab for visualizing an AugmentedImage.
    5.         /// </summary>
    6.         public AugmentedImageVisualizer[] AugmentedImageVisualizerPrefab;
    7.  
    8.         /// <summary>
    9.         /// The overlay containing the fit to scan user guide.
    10.         /// </summary>
    11.         public GameObject FitToScanOverlay;
    12.  
    13.         private Dictionary<int, AugmentedImageVisualizer> m_Visualizers
    14.             = new Dictionary<int, AugmentedImageVisualizer>();
    15.  
    16.         private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();
    17.  
    18.         /// <summary>
    19.         /// The Unity Update method.
    20.         /// </summary>
    21.         public void Update()
    22.         {
    23.             // Exit the app when the 'back' button is pressed.
    24.             if (Input.GetKey(KeyCode.Escape))
    25.             {
    26.                 Application.Quit();
    27.             }
    28.  
    29.             // Get updated augmented images for this frame.
    30.             Session.GetTrackables<AugmentedImage>(
    31.                 m_TempAugmentedImages, TrackableQueryFilter.Updated);
    32.  
    33.  
    34.  
    35.             // Create visualizers and anchors for updated augmented images that are tracking and do
    36.             // not previously have a visualizer. Remove visualizers for stopped images.
    37.             foreach (var image in m_TempAugmentedImages)
    38.             {
    39.                 int i = image.DatabaseIndex;
    40.                 AugmentedImageVisualizer visualizer = null;
    41.                 m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
    42.                 if (image.TrackingState == TrackingState.Tracking && visualizer == null)
    43.                 {
    44.                     // Create an anchor to ensure that ARCore keeps tracking this augmented image.
    45.                     Anchor anchor = image.CreateAnchor(image.CenterPose);
    46.  
    47.                     visualizer = (AugmentedImageVisualizer)Instantiate(
    48.                         AugmentedImageVisualizerPrefab[i], anchor.transform);
    49.                     visualizer.Image = image;
    50.                     m_Visualizers.Add(image.DatabaseIndex, visualizer);
    51.                 }
    52.                 else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
    53.                 {
    54.                     m_Visualizers.Remove(image.DatabaseIndex);
    55.                     GameObject.Destroy(visualizer.gameObject);
    56.  
    57.  
    58.                 }
    59.              
    60.             }
    61.  
    62.             // Show the fit-to-scan overlay if there are no images that are Tracking.
    63.             foreach (var visualizer in m_Visualizers.Values)
    64.             {
    65.                 if (visualizer.Image.TrackingState == TrackingState.Tracking)
    66.                 {
    67.                     FitToScanOverlay.SetActive(false);
    68.                     return;
    69.                 }
    70.             }
    71.  
    72.             FitToScanOverlay.SetActive(true);
    73.         }
    74.     }
    75. }
    76.  
    When I tested this the prefab sometimes comes while showing the image sometimes it wont.After showing it to two images the app crashes.Why the prefab does not go even after the ImageTarget is removed.
     
    Last edited: Jun 18, 2019
  2. HareshV

    HareshV

    Joined:
    Feb 25, 2019
    Posts:
    1
    Why the prefab does not go even after the ImageTarget is removed? for this

    try using TrackingMethod where ever you are using TrackingState . Both in AugmentedImageExampleController and AugmentedImageVisualizer scripts

    TrackingMethod
    has 3 states FullTracking,NotTracking, LastKnowPose.

    for eg: the below if statement will be

    if (image.TrackingState == TrackingState.Tracking && visualizer == null){}

    change to

    if (image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking && visualizer == null){}