Search Unity

ARFoundation - Image Tracking - Different prefabs for different images

Discussion in 'AR' started by BMythes, Aug 16, 2019.

  1. BMythes

    BMythes

    Joined:
    Feb 20, 2018
    Posts:
    6
    Hey all.

    I have been creating an AR card-based experience, with a simple concept like bringing cards to life.
    For that, I would need to have different cards being read and instantiating different prefabs. I was able to get multiple cards being read but they all instantiate the same prefab.

    I looked into using ARFoundation for this but I couldn't find any information on instantiating different prefabs, just tidbits of information here and there. Would anyone have a good idea of how to proceed with this?

    Thank you!
     
  2. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    Subscribe to an event like:
    Code (CSharp):
    1.  void UpdateTrackedImage(ARTrackedImage trackedImage)
    . When the image is added or updated, check it's name and use that to Instantiate and object if it is a match.
     
  3. BMythes

    BMythes

    Joined:
    Feb 20, 2018
    Posts:
    6
    But should I add that onto a separate script or append to the one already existing within ARFoundation?
    Sorry, I'm still a beginner at coding, so I just want to make sure before I break something haha
     
  4. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    I have a script in this post that shows a complete example. No, you should not alter that script since updating to the latest version of ARFoundation will overwrite your changes. Make a new script, and subscibe to the events as I did in the OnEnable function. It's also good practice to unsubscrine in OnDisable as in my example.

    Whenever that event happens, the function that subscribed to the event will run. It does require that the script exists in the scene, that's what I am doing in the Awake, looking for the script so that it can subscribe to it's event.

    Your function needs to loop through all the images looking for a specific name.
     
  5. BMythes

    BMythes

    Joined:
    Feb 20, 2018
    Posts:
    6
    That was perfectly explained @Voronoi , thank you VERY much.

    I'll give this a try and update this thread.

    Sorry for the long delay, I don't get notifications from this forum sometimes..
     
  6. BMythes

    BMythes

    Joined:
    Feb 20, 2018
    Posts:
    6
    @Voronoi

    I wasn't able to get it to work.. I created a Dictionary and added to my XRReferenceLibrary all the images that would "spawn" the objects (they are just three circles, with different colored fills).

    Yet now my application recognizes none of the images nor spawns any objects... Any chance you or someone could give me some help or point me in the right direction?

    Thanks.


    This is the full code, adapted from both yours and another user's here on the Forum...
    This script is added on AR Session Origin, alongside the AR Image Track Manager, AR Reference Point Manager, and AR Tracked Image Info Manager.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARSubsystems;
    6. using UnityEngine.XR.ARFoundation;
    7.  
    8. public class ARPerImage : MonoBehaviour
    9. {
    10.  
    11. Dictionary<string, GameObject> myPrefabsDictionary = new Dictionary<string, GameObject>();
    12.  
    13. List<GameObject> o = new List<GameObject>();
    14.  
    15.  
    16.     ARTrackedImageManager m_TrackedImageManager;
    17.  
    18.     private void Awake()
    19.     {
    20.         m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
    21.     }
    22.  
    23.     void OnEnable()
    24.     {
    25.         m_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    26.     }
    27.  
    28.     void OnDisable()
    29.     {
    30.         m_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    31.     }
    32.  
    33.  
    34.     // Start is called before the first frame update
    35.     void Start()
    36.     {
    37.         myPrefabsDictionary.Add("oldWell", Resources.Load("oldWell") as GameObject);
    38.         myPrefabsDictionary.Add("redSphere", Resources.Load("redSphere") as GameObject);
    39.         myPrefabsDictionary.Add("blueSphere", Resources.Load("blueSphere") as GameObject);
    40.         myPrefabsDictionary.Add("greenSphere", Resources.Load("greenSphere") as GameObject);
    41.     }
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.      
    46.     }
    47.  
    48.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    49.     {
    50.         ARTrackedImage trackedImage = null;
    51.         // Checks the new tracked images
    52.         for (int i = 0; i < eventArgs.added.Count; i++)
    53.         {
    54.             trackedImage = eventArgs.added[i];
    55.             string imgName = trackedImage.referenceImage.name; // this is the name of image in the library
    56.             GameObject prefab = myPrefabsDictionary[imgName];
    57.             o.Add(Instantiate(prefab, trackedImage.transform));
    58.         }
    59.      
    60.         for (int i = 0; i < eventArgs.updated.Count; i++)
    61.         {
    62.             trackedImage = eventArgs.updated[i];
    63.             if (trackedImage.trackingState == TrackingState.Tracking)
    64.                 o[i].SetActive(true);
    65.             else
    66.                 o[i].SetActive(false);
    67.  
    68.         }
    69.      
    70.         for (int i = 0; i < eventArgs.removed.Count; i++)
    71.         {
    72.                 o[i].SetActive(false);
    73.  
    74.         }
    75.     }
    76. }
     
  7. SKSK123

    SKSK123

    Joined:
    Feb 26, 2019
    Posts:
    1
    @BMythes
    I was wondering if you now know how to use different prefabs on different images?
    Looking for a solution.
     
  8. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324