Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created August 11, 2021 12:36
Show Gist options
  • Save andijakl/3f384f724d872d7649a3ed2cacf4be8d to your computer and use it in GitHub Desktop.
Save andijakl/3f384f724d872d7649a3ed2cacf4be8d to your computer and use it in GitHub Desktop.
Reacting to new tracked images in AR Foundation (part 1)
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
   // Go through all tracked images that have been added
    // (-> new markers detected)
    foreach (var trackedImage in eventArgs.added) 
{
        // Get the name of the reference image to search for the corresponding prefab
        var imageName = trackedImage.referenceImage.name;
        foreach (var curPrefab in ArPrefabs)
{
            if (string.Compare(curPrefab.name, imageName, StringComparison.Ordinal) == 0 
                && !_instantiatedPrefabs.ContainsKey(imageName))
            {
                // Found a corresponding prefab for the reference image, and it has not been 
// instantiated yet > new instance, with the ARTrackedImage as parent 
// (so it will automatically get updated when the marker changes in real life)
 var newPrefab = Instantiate(curPrefab, trackedImage.transform);
                // Store a reference to the created prefab
                _instantiatedPrefabs[imageName] = newPrefab;
 }
        }
    }
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment