Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question AR Foundation - Multiple Tracked Image AR project not working

Discussion in 'AR' started by aneri1997, Aug 21, 2023.

  1. aneri1997

    aneri1997

    Joined:
    Apr 5, 2020
    Posts:
    2
    I have a AR project in which there are multiple image targets which is being tracked and their corresponding prefabs that gets instantiated once the image is tracked. As per the script, the activation/deactivation of the prefab depends upon the tracked image's tracking state. Now what I want is, to keep a particular prefab activated even if that tracked image's tracking is lost, and that prefab should be deactivated only when any other target images are tracked. This is the script, what changes should I make to achieve this??


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6. using UnityEngine.Events;
    7. using TMPro;
    8.  
    9. [RequireComponent(typeof(ARTrackedImageManager))]
    10. public class PlaceTrackedImages : MonoBehaviour
    11. {
    12.     private ARTrackedImageManager _trackedImagesManager;
    13.     public GameObject[] ArPrefabs;
    14.     private readonly Dictionary<string, GameObject> _instantiatedPrefabs = new Dictionary<string, GameObject>();
    15.     public UnityEvent order;
    16.     public TextMeshProUGUI status;
    17.  
    18.     private void Awake()
    19.     {
    20.         _trackedImagesManager = GetComponent<ARTrackedImageManager>();
    21.     }
    22.  
    23.     private void OnEnable()
    24.     {
    25.         _trackedImagesManager.trackedImagesChanged += OnTrackedImagesChanged;
    26.     }
    27.  
    28.     private void OnDisable()
    29.     {
    30.         _trackedImagesManager.trackedImagesChanged -= OnTrackedImagesChanged;
    31.     }
    32.  
    33.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    34.     {
    35.         // New tracked images
    36.         foreach (var trackedImage in eventArgs.added)
    37.         {
    38.             var imageName = trackedImage.referenceImage.name;
    39.             foreach (var curPrefab in ArPrefabs)
    40.             {
    41.                 if (string.Compare(curPrefab.name, imageName, System.StringComparison.OrdinalIgnoreCase) == 0
    42.                     && !_instantiatedPrefabs.ContainsKey(imageName))
    43.                 {
    44.                     var newPrefab = Instantiate(curPrefab, trackedImage.transform);
    45.                     order.Invoke();
    46.                     _instantiatedPrefabs[imageName] = newPrefab;
    47.                 }
    48.             }
    49.         }
    50.  
    51.         // Updated tracked images
    52.         foreach (var trackedImage in eventArgs.updated)
    53.         {
    54.             if (_instantiatedPrefabs.TryGetValue(trackedImage.referenceImage.name, out var prefab))
    55.             {
    56.                 status.text = trackedImage.referenceImage.name;
    57.                 prefab.SetActive(trackedImage.trackingState == TrackingState.Tracking);
    58.             }
    59.         }
    60.  
    61.         // Removed tracked images
    62.         foreach (var trackedImage in eventArgs.removed)
    63.         {
    64.             if (_instantiatedPrefabs.TryGetValue(trackedImage.referenceImage.name, out var prefab))
    65.             {
    66.                 Destroy(prefab);
    67.                 _instantiatedPrefabs.Remove(trackedImage.referenceImage.name);
    68.             }
    69.         }
    70.     }
    71. }