Search Unity

AR Foundation - Multiple Tracked Image Managers?

Discussion in 'Handheld AR' started by drewfjacobs, Jun 19, 2019.

  1. drewfjacobs

    drewfjacobs

    Joined:
    Mar 12, 2019
    Posts:
    6
    I am trying to have different prefabs spawn for different images tracked. Would it it possible to just add a second AR Tracked Image Manager with a separate Reference Library and Tracked Image Prefab assigned to it in order to achieve this goal?
     
    mtessmann and ercuesy like this.
  2. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    554
    The AR managers are effectively singletons, so you should generally not use two of them. You can, however, set the active reference image library and the prefab at runtime.

    Another option to achieve this might be to use a prefab which just contains a single MonoBehaviour script that can decide which other prefab to instantiate as a child of itself.
     
  3. BMythes

    BMythes

    Joined:
    Feb 20, 2018
    Posts:
    6
    Sorry for reviving this thread, I'm just working on exactly the same thing you described here.

    A solution I was thinking was instantiating a Gameobject A that would be a parent of different prefabs B, C and D (all child prefabs start as inactive). And then, depending on which image is read from the library, it activates one of the prefabs.

    I also thought about having the library of images become an array, and associate that array with an array of prefabs. Hence, Image [0] would instantiate Prefab [0], etc.

    Are any of these solutions valid, and could you possibly point towards how to solve them? I'm still a beginner at code having a hard time with editing ARFoundation's code.

    Thank you!
     
    ercuesy likes this.
  4. ercuesy

    ercuesy

    Joined:
    Jul 6, 2019
    Posts:
    12
    Has anyone been able to figure this one out? I tried tdmowrer approach but ended up crashing the app. Is there an example of how to do it?
     
  5. jpvanmuijen

    jpvanmuijen

    Joined:
    Aug 23, 2012
    Posts:
    7
  6. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    554
    In ARFoundation 3.0, it is now possible to add an ARTrackedObjectManager and then set its reference object library. This wasn't possible before because it would throw if enabled without a reference object library. From the changelog:
    What is the callstack?
     
    ercuesy likes this.
  7. ercuesy

    ercuesy

    Joined:
    Jul 6, 2019
    Posts:
    12
    @tdmowrer I actually figured that an unused Wikitude library reference I was playing with was causing the crash, sorry for the confusion. I'll try the approach given by @jpvanmuijen and see how it goes, thanks!
     
  8. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    64
    I think it's a must-have to be able to set a different prefab for each trackable image. Otherwise, this feature is pretty useless. Yes, you could do this by letting a single prefab decide what to spawn below it, but that is a quite weird workaround.

    Actually, the approach Vuforia is following here makes more sense to me: The game object is created first, and then a tracking script is used on top of that game object which references the trackable image. So each time this image is recognized, the game object will be activated and placed at the image's position.
     
    apiotuch likes this.
  9. ayagiku

    ayagiku

    Joined:
    Jan 17, 2020
    Posts:
    1
    I found TrackedImageManager has only a single prefab just now (3.0). When multiple trackers are set TrackedImageManager , the manager shows multiple same prefabs.
    Whether I looked for codes that instanciate the prefab to modify the manager into be able to handle different prefabs, couldn't find it in the class. The instanciation codes may be written in any other location which catches tracking status updated event object. Do anybodies know how to show defferent prefabs by tracked images?

    best regards.
     
  10. Markus_T

    Markus_T

    Joined:
    Jan 23, 2014
    Posts:
    3
    You can track a separate object with each tracking image using this script.
    Add this script to your AR Session Origin gameObject and make sure that the Objects To Place list has the same number of gameObjects as there are images in your Reference Image Library. For this script, the gameObjects must be in your scene.

    You should also set the Tracked Image Prefab in the AR Tracked Image Manager to None.

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6.  
    7. [RequireComponent(typeof(ARTrackedImageManager))]
    8. public class TrackMultiple : MonoBehaviour
    9. {
    10.     [Header("The length of this list must match the number of images in Reference Image Library")]
    11.     public List<GameObject> ObjectsToPlace;
    12.  
    13.     private int refImageCount;
    14.     private Dictionary<string, GameObject> allObjects;
    15.     private ARTrackedImageManager arTrackedImageManager;
    16.     private IReferenceImageLibrary refLibrary;
    17.  
    18.     void Awake()
    19.     {
    20.         arTrackedImageManager = GetComponent<ARTrackedImageManager>();
    21.     }
    22.  
    23.     private void OnEnable()
    24.     {
    25.         arTrackedImageManager.trackedImagesChanged += OnImageChanged;
    26.     }
    27.  
    28.     private void OnDisable()
    29.     {
    30.         arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
    31.     }
    32.  
    33.     private void Start()
    34.     {
    35.         refLibrary = arTrackedImageManager.referenceLibrary;
    36.         refImageCount = refLibrary.count;
    37.         LoadObjectDictionary();
    38.     }
    39.  
    40.     void LoadObjectDictionary()
    41.     {
    42.         allObjects = new Dictionary<string, GameObject>();
    43.         for (int i = 0; i < refImageCount; i++)
    44.         {
    45.             allObjects.Add(refLibrary[i].name, ObjectsToPlace[i]);
    46.             ObjectsToPlace[i].SetActive(false);
    47.         }
    48.     }
    49.  
    50.     void ActivateTrackedObject(string _imageName)
    51.     {
    52.         allObjects[_imageName].SetActive(true);
    53.     }
    54.  
    55.     public void OnImageChanged(ARTrackedImagesChangedEventArgs _args)
    56.     {
    57.         foreach (var addedImage in _args.added)
    58.         {
    59.             ActivateTrackedObject(addedImage.referenceImage.name);
    60.         }
    61.  
    62.         foreach (var updated in _args.updated)
    63.         {
    64.             allObjects[updated.referenceImage.name].transform.position = updated.transform.position;
    65.             allObjects[updated.referenceImage.name].transform.rotation = updated.transform.rotation;
    66.         }
    67.     }
    68. }
    69.  
     
    Ludibell and mtessmann like this.
unityunity