Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Issue with ARTrackedImageManager with AR Foundation 6.0

Discussion in 'AR' started by tperez0711, May 12, 2024.

  1. tperez0711

    tperez0711

    Joined:
    Mar 11, 2019
    Posts:
    1
    So i recently upgraded my AR Foundation to 6.0 from 5.01 because I updated to URP and read 6.0 works well with URP.

    The issue I'm having now is when i run in XR simulation I'm no longer able to track my image targets.

    I updated my code to use trackablesChanges.AddListener and RemoveListener as well as changing to
    ARTrackablesChangedEventArgs<ARTrackedImage> eventArgs

    Here is the code where I'm using the ARTrackedImageManager


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.ARFoundation;
    3. using UnityEngine.XR.ARSubsystems;
    4.  
    5. public class ImageToPrefab : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private ARTrackedImageManager trackedImageManager;
    9.  
    10.     // Define public variables for the prefabs
    11.     public GameObject prefab1;
    12.     public GameObject prefab2;
    13.     public GameObject prefab3;
    14.  
    15.     // Dictionary to hold the mappings from image names to prefab GameObjects
    16.     public Dictionary<string, GameObject> imageToPrefab;
    17.  
    18.     // Dictionary to keep track of instantiated prefabs
    19.     private Dictionary<string, GameObject> instantiatedPrefabs = new Dictionary<string, GameObject>();
    20.  
    21.     private void Awake()
    22.     {
    23.         // Initialize the dictionary based on your specific needs
    24.         imageToPrefab = new Dictionary<string, GameObject>()
    25.         {
    26.             { "one", prefab1 },
    27.             { "two", prefab2 },
    28.             { "Tiki coaster", prefab3},
    29.             // Add more mappings as needed
    30.         };
    31.     }
    32.     void Start() {
    33.     Debug.Log($"Library contains {trackedImageManager.referenceLibrary.count} images.");
    34. }
    35.  
    36.     private void OnEnable() => trackedImageManager.trackablesChanged.AddListener(OnImageChanged);
    37.  
    38.     private void OnDisable() => trackedImageManager.trackablesChanged.RemoveListener(OnImageChanged);
    39.    
    40.     void OnImageChanged(ARTrackablesChangedEventArgs<ARTrackedImage> eventArgs)
    41.     {
    42.         foreach (var trackedImage in eventArgs.added)
    43.         {
    44.             InstantiatePrefabForImage(trackedImage);
    45.         }
    46.  
    47.         foreach (var trackedImage in eventArgs.updated)
    48.         {
    49.             if (trackedImage.trackingState == TrackingState.Tracking)
    50.             {
    51.                 InstantiatePrefabForImage(trackedImage);
    52.             }
    53.             else if (trackedImage.trackingState == TrackingState.None)
    54.             {
    55.                 RemovePrefabForImage(trackedImage);
    56.             }
    57.         }
    58.  
    59.         foreach (var trackedImage in eventArgs.removed)
    60.         {
    61.             RemovePrefabForImage(trackedImage);
    62.         }
    63.     }
    64.  
    65.     private void InstantiatePrefabForImage(ARTrackedImage trackedImage)
    66.     {
    67.         string imageName = trackedImage.referenceImage.name;
    68.         if (imageToPrefab.ContainsKey(imageName) && !instantiatedPrefabs.ContainsKey(imageName))
    69.         {
    70.             var prefab = imageToPrefab[imageName];
    71.             var instance = Instantiate(prefab, trackedImage.transform.position, trackedImage.transform.rotation);
    72.             instantiatedPrefabs[imageName] = instance;
    73.             instance.transform.SetParent(trackedImage.transform);
    74.         }
    75.     }
    76.  
    77.     private void RemovePrefabForImage(ARTrackedImage trackedImage)
    78.     {
    79.         string imageName = trackedImage.referenceImage.name;
    80.         if (instantiatedPrefabs.ContainsKey(imageName))
    81.         {
    82.             var instance = instantiatedPrefabs[imageName];
    83.             Destroy(instance);
    84.             instantiatedPrefabs.Remove(imageName);
    85.         }
    86.     }
    87.  
    88.     void Update()
    89.     {
    90.         if(Input.GetKeyUp(KeyCode.Space)){
    91.             ListAllImages();
    92.         }
    93.  
    94.     }
    95.  
    96.     void ListAllImages()
    97.     {
    98.         Debug.Log(
    99.             $"There are {trackedImageManager.trackables.count} images being tracked.");
    100.  
    101.         foreach (var trackedImage in trackedImageManager.trackables)
    102.         {
    103.             Debug.Log($"Image: {trackedImage.referenceImage.name} is at " +
    104.                       $"{trackedImage.transform.position}");
    105.         }
    106.     }
    107. }
    108.  
    When i debug it i get through OnEnable but i never break into OnImageChanged. To note this was working perfectly fine before i updated to 6.0. Image Reference library is loading in all the images just nothing is getting tracked.

    One thing im curious about that is new is with the attached image. The " trackables changed (ARTrackablesChangedEventArgs`1)" inside the AR Tracked Image Manager component list is new. Not sure if it has anything to do with my issue
     

    Attached Files:

  2. mike-copley-unity

    mike-copley-unity

    Unity Technologies

    Joined:
    Dec 7, 2023
    Posts:
    17
    Hello @tperez0711

    The screenshot you provided for the 'trackablesChanged' event looks fine. That list purely represents the set of serialized event handlers that have been directly linked through the Unity Editor.

    My first recommendation for you would be to try out the BasicImageTracking AR Foundation sample within the Editor using the 'Billboard City' simulation environment.

    1. Load the scene 'BasicImageTracking' into the Editor
    2. Add all of the images (through the UI button)
    3. Navigate the simulation environment by holding the right mouse button and using the mouse to look around, and the 'WASD' keys to move about
    4. Look up at the building sides and see the images get covered with an image info billboard.
    5. Modify the code behind this sample to see the trackables changed, and help diagnose your issue

    I hope this helps resolve things for you.