using System; using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; public class ImageTrackingObjectManager : MonoBehaviour { [SerializeField] [Tooltip("Image manager on the AR Session Origin")] ARTrackedImageManager m_ImageManager; /// /// Get the ARTrackedImageManager /// public ARTrackedImageManager ImageManager { get => m_ImageManager; set => m_ImageManager = value; } [SerializeField] [Tooltip("Reference Image Library")] XRReferenceImageLibrary m_ImageLibrary; /// /// Get the XRReferenceImageLibrary /// public XRReferenceImageLibrary ImageLibrary { get => m_ImageLibrary; set => m_ImageLibrary = value; } [SerializeField] [Tooltip("Prefab for tracked 1 image")] GameObject m_OnePrefab; /// /// Get the one prefab /// public GameObject onePrefab { get => m_OnePrefab; set => m_OnePrefab = value; } GameObject m_SpawnedOnePrefab; /// /// get the spawned one prefab /// public GameObject spawnedOnePrefab { get => m_SpawnedOnePrefab; set => m_SpawnedOnePrefab = value; } [SerializeField] [Tooltip("Prefab for tracked 2 image")] GameObject m_TwoPrefab; /// /// get the two prefab /// public GameObject twoPrefab { get => m_TwoPrefab; set => m_TwoPrefab = value; } GameObject m_SpawnedTwoPrefab; /// /// get the spawned two prefab /// public GameObject spawnedTwoPrefab { get => m_SpawnedTwoPrefab; set => m_SpawnedTwoPrefab = value; } int m_NumberOfTrackedImages; NumberManager m_OneNumberManager; NumberManager m_TwoNumberManager; static Guid s_FirstImageGUID; static Guid s_SecondImageGUID; void OnEnable() { s_FirstImageGUID = m_ImageLibrary[0].guid; s_SecondImageGUID = m_ImageLibrary[1].guid; m_ImageManager.trackedImagesChanged += ImageManagerOnTrackedImagesChanged; } void OnDisable() { m_ImageManager.trackedImagesChanged -= ImageManagerOnTrackedImagesChanged; } void ImageManagerOnTrackedImagesChanged(ARTrackedImagesChangedEventArgs obj) { // added, spawn prefab foreach(ARTrackedImage image in obj.added) { if (image.referenceImage.guid == s_FirstImageGUID) { m_SpawnedOnePrefab = Instantiate(m_OnePrefab, image.transform.position, image.transform.rotation); m_OneNumberManager = m_SpawnedOnePrefab.GetComponent(); } else if (image.referenceImage.guid == s_SecondImageGUID) { m_SpawnedTwoPrefab = Instantiate(m_TwoPrefab, image.transform.position, image.transform.rotation); m_TwoNumberManager = m_SpawnedTwoPrefab.GetComponent(); } } // updated, set prefab position and rotation foreach(ARTrackedImage image in obj.updated) { // image is tracking or tracking with limited state, show visuals and update it's position and rotation if (image.trackingState == TrackingState.Tracking) { if (image.referenceImage.guid == s_FirstImageGUID) { m_OneNumberManager.Enable3DNumber(true); m_SpawnedOnePrefab.transform.SetPositionAndRotation(image.transform.position, image.transform.rotation); } else if (image.referenceImage.guid == s_SecondImageGUID) { m_TwoNumberManager.Enable3DNumber(true); m_SpawnedTwoPrefab.transform.SetPositionAndRotation(image.transform.position, image.transform.rotation); } } // image is no longer tracking, disable visuals TrackingState.Limited TrackingState.None else { if (image.referenceImage.guid == s_FirstImageGUID) { m_OneNumberManager.Enable3DNumber(false); } else if (image.referenceImage.guid == s_SecondImageGUID) { m_TwoNumberManager.Enable3DNumber(false); } } } // removed, destroy spawned instance foreach(ARTrackedImage image in obj.removed) { if (image.referenceImage.guid == s_FirstImageGUID) { Destroy(m_SpawnedOnePrefab); } else if (image.referenceImage.guid == s_FirstImageGUID) { Destroy(m_SpawnedTwoPrefab); } } } public int NumberOfTrackedImages() { m_NumberOfTrackedImages = 0; foreach (ARTrackedImage image in m_ImageManager.trackables) { if (image.trackingState == TrackingState.Tracking) { m_NumberOfTrackedImages++; } } return m_NumberOfTrackedImages; } }