Skip to content

Instantly share code, notes, and snippets.

@andijakl
Last active August 13, 2021 07:22
Show Gist options
  • Save andijakl/5a8563b52536821889dea40ae0ffcea9 to your computer and use it in GitHub Desktop.
Save andijakl/5a8563b52536821889dea40ae0ffcea9 to your computer and use it in GitHub Desktop.
Reacting to updated and removed tracked images in AR Foundation (part 2)
// Disable instantiated prefabs that are no longer being actively tracked
foreach (var trackedImage in eventArgs.updated) {
    _instantiatedPrefabs[trackedImage.referenceImage.name]
        .SetActive(trackedImage.trackingState == TrackingState.Tracking);
}
// Remove is called if the subsystem has given up looking for the trackable again.
// (If it's invisible, its tracking state would just go to limited initially).
// Note: ARCore doesn't seem to remove these at all; if it does, it would delete our child GameObject
// as well.
foreach (var trackedImage in eventArgs.removed) {
// Destroy the instance in the scene.
// Note: this code does not delete the ARTrackedImage parent, which was created
// by AR Foundation, is managed by it and should therefore also be deleted by AR Foundation.
Destroy(_instantiatedPrefabs[trackedImage.referenceImage.name]);
// Also remove the instance from our array
_instantiatedPrefabs.Remove(trackedImage.referenceImage.name);
// Alternative: do not destroy the instance, just set it inactive
//_instantiatedPrefabs[trackedImage.referenceImage.name].SetActive(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment