Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question AR Foundation - Update Tracked Image Prefab in AR Tracked Image Manager during Runtime?

Discussion in 'AR' started by emilibroooo, Feb 10, 2021.

  1. emilibroooo

    emilibroooo

    Joined:
    Jan 27, 2018
    Posts:
    4
    So I've seen some tutorials on how to have multiple tracked image prefabs with different image targets in your reference image library. My goal is to switch between multiple tracked image prefabs (one at a time) with the same image target.

    I have this script attached to the AR Session Origin
    Code (CSharp):
    1. public class ClothingLineController : MonoBehaviour
    2. {
    3.     // Create an array of GameObjects that will be initialized in the Unity Editor
    4.     [SerializeField]
    5.     private GameObject[] clothingModels;
    6.  
    7.     private ARTrackedImageManager trackedImageManager;
    8.     private int index = 0;
    9.  
    10.     private void Awake()
    11.     {
    12.         trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    13.         trackedImageManager.trackedImagePrefab = clothingModels[index];
    14.     }
    15.  
    16.     public void NextButton()
    17.     {
    18.         if (index < clothingModels.Length - 1)
    19.         {
    20.             index++;
    21.         }
    22.         else
    23.         {
    24.             index = 0;
    25.         }
    26.         // Update tracked image prefab
    27.         trackedImageManager.trackedImagePrefab = clothingModels[index];
    28.     }
    29.  
    30.     public void PreviousButton()
    31.     {
    32.         if(index > 0)
    33.         {
    34.             index--;
    35.         } else
    36.         {
    37.             index = clothingModels.Length - 1;
    38.         }
    39.         // Update tracked image prefab
    40.         trackedImageManager.trackedImagePrefab = clothingModels[index];
    41.     }
    42. }
    And this will update the the tracked image prefab in my Unity Editor when I run it. However, when I build the app to my device, the model does not update.

    Is there a way to reload the ARTrackedImageManager after I change the tracked image prefab? Or specifically just the tracked image prefab? Or if anyone has a different solution that would be great!
     
  2. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    The Prefab is pulled directly from the ARTrackedImageManager upon Instantiation of the Object.
    Are you trying to change the model AFTER the image has been found/added?
    If so: Changing the Prefab won't help you. It's already been spawned.