Created
June 27, 2022 20:25
-
-
Save alastaira/92d790ed09330ea7a45e7c3a2a4d26e1 to your computer and use it in GitHub Desktop.
Unity 2022 Augmented Reality Image Tracking Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://youtu.be/gpaq5bAjya8 for accompanying tutorial and usage! | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.XR.ARFoundation; | |
using UnityEngine.XR.ARSubsystems; | |
[RequireComponent(typeof(ARTrackedImageManager))] | |
public class PlaceTrackedImages : MonoBehaviour { | |
// Reference to AR tracked image manager component | |
private ARTrackedImageManager _trackedImagesManager; | |
// List of prefabs to instantiate - these should be named the same | |
// as their corresponding 2D images in the reference image library | |
public GameObject[] ArPrefabs; | |
// Keep dictionary array of created prefabs | |
private readonly Dictionary<string, GameObject> _instantiatedPrefabs = new Dictionary<string, GameObject>(); | |
void Awake() { | |
// Cache a reference to the Tracked Image Manager component | |
_trackedImagesManager = GetComponent<ARTrackedImageManager>(); | |
} | |
void OnEnable() { | |
// Attach event handler when tracked images change | |
_trackedImagesManager.trackedImagesChanged += OnTrackedImagesChanged; | |
} | |
void OnDisable() { | |
// Remove event handler | |
_trackedImagesManager.trackedImagesChanged -= OnTrackedImagesChanged; | |
} | |
// Event Handler | |
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) { | |
// Loop through all new tracked images that have been detected | |
foreach (var trackedImage in eventArgs.added) { | |
// Get the name of the reference image | |
var imageName = trackedImage.referenceImage.name; | |
// Now loop over the array of prefabs | |
foreach (var curPrefab in ArPrefabs) { | |
// Check whether this prefab matches the tracked image name, and that | |
// the prefab hasn't already been created | |
if (string.Compare(curPrefab.name, imageName, StringComparison.OrdinalIgnoreCase) == 0 | |
&& !_instantiatedPrefabs.ContainsKey(imageName)) { | |
// Instantiate the prefab, parenting it to the ARTrackedImage | |
var newPrefab = Instantiate(curPrefab, trackedImage.transform); | |
// Add the created prefab to our array | |
_instantiatedPrefabs[imageName] = newPrefab; | |
} | |
} | |
} | |
// For all prefabs that have been created so far, set them active or not depending | |
// on whether their corresponding image is currently being tracked | |
foreach (var trackedImage in eventArgs.updated) { | |
_instantiatedPrefabs[trackedImage.referenceImage.name] | |
.SetActive(trackedImage.trackingState == TrackingState.Tracking); | |
} | |
// If the AR subsystem has given up looking for a tracked image | |
foreach (var trackedImage in eventArgs.removed) { | |
// Destroy its prefab | |
Destroy(_instantiatedPrefabs[trackedImage.referenceImage.name]); | |
// Also remove the instance from our array | |
_instantiatedPrefabs.Remove(trackedImage.referenceImage.name); | |
// Or, simply set the prefab instance to inactive | |
//_instantiatedPrefabs[trackedImage.referenceImage.name].SetActive(false); | |
} | |
} | |
} |
Try looking at adb logcat
, errors may popup. In my there is an empty access to a list
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I have a question... I followed all the steps explained in the YouTube video but for some reason when I use the application on my phone it doesn't show me any model in AR when I put the camera on the image that I previously saved in Unity. Would you know what the problem is or what am I missing?