1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-24 08:08:26 -05:00

119 lines
4.0 KiB
C#
Raw Normal View History

// NOTE: Taken from https://github.com/dilmerv/UnityARFoundationEssentials/blob/master/Assets/Scripts/TrackedImageInfoMultipleManager.cs
// This has been modified to fit the need of the UDRI XRTeam's use-cases
2024-04-16 15:39:29 -04:00
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
2024-04-16 15:39:29 -04:00
using UnityEngine;
using UnityEngine.XR.ARFoundation;
2024-04-17 10:43:07 -04:00
namespace MAVRIC.GEEEKCup.Samples.QRCode
2024-04-16 15:39:29 -04:00
{
public class TrackedImageChanged : MonoBehaviour
{
[SerializeField] private ARTrackedImageManager trackedImageManager;
[SerializeField] private GameObject[] arPrefabs;
2024-04-16 15:39:29 -04:00
[SerializeField] private float3 scaleFactor = new (0.1f, 0.1f, 0.1f);
2024-04-16 15:39:29 -04:00
private Dictionary<string, GameObject> arObjects = new ();
2024-04-16 15:39:29 -04:00
private void Awake()
2024-04-16 15:39:29 -04:00
{
if (trackedImageManager == null)
{
if (!TryGetComponent(out trackedImageManager))
{
2024-04-17 10:43:07 -04:00
Debug.LogError("Failed to find ARTrackedImageManager", this.gameObject);
}
}
2024-04-16 15:39:29 -04:00
}
private void OnEnable()
2024-04-16 15:39:29 -04:00
{
if (trackedImageManager == null) return;
trackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
2024-04-16 15:39:29 -04:00
SpawnAllARObjects();
2024-04-16 15:39:29 -04:00
}
private void OnDisable()
2024-04-16 15:39:29 -04:00
{
if (trackedImageManager == null) return;
trackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
2024-04-16 15:39:29 -04:00
for (int i = arObjects.Keys.Count - 1; i >= 0; i--)
{
var key = arObjects.Keys.ElementAt(i);
if (arObjects.Remove(key, out var value))
2024-04-16 15:39:29 -04:00
{
Destroy(value);
2024-04-16 15:39:29 -04:00
}
}
}
private void SpawnAllARObjects()
{
// setup all game objects in dictionary
foreach (GameObject arObject in arPrefabs)
2024-04-16 15:39:29 -04:00
{
GameObject newARObject = Instantiate(arObject, Vector3.zero, Quaternion.identity);
newARObject.SetActive(false);
2024-04-16 15:39:29 -04:00
newARObject.name = arObject.name;
arObjects.Add(arObject.name, newARObject);
2024-04-16 15:39:29 -04:00
}
}
2024-04-16 15:39:29 -04:00
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (ARTrackedImage trackedImage in eventArgs.added)
2024-04-16 15:39:29 -04:00
{
UpdateARImage(trackedImage);
}
2024-04-16 15:39:29 -04:00
foreach (ARTrackedImage trackedImage in eventArgs.updated)
{
UpdateARImage(trackedImage);
2024-04-16 15:39:29 -04:00
}
foreach (ARTrackedImage trackedImage in eventArgs.removed)
2024-04-16 15:39:29 -04:00
{
arObjects[trackedImage.name].SetActive(false);
2024-04-16 15:39:29 -04:00
}
}
private void UpdateARImage(ARTrackedImage trackedImage)
2024-04-16 15:39:29 -04:00
{
// Assign and Place Game Object
AssignGameObject(trackedImage.referenceImage.name, trackedImage.transform.position, scaleFactor, trackedImage.transform.rotation);
Debug.Log($"trackedImage.referenceImage.name: {trackedImage.referenceImage.name}");
2024-04-16 15:39:29 -04:00
}
private void AssignGameObject(string name, Vector3 newPosition, Vector3 newScale, Quaternion newRotation)
{
if (arPrefabs != null)
{
GameObject goARObject = arObjects[name];
goARObject.SetActive(true);
goARObject.transform.position = newPosition;
goARObject.transform.localScale = newScale;
goARObject.transform.localRotation = newRotation;
foreach (GameObject go in arObjects.Values)
{
Debug.Log($"Go in arObjects.Values: {go.name}");
if (go.name != name)
{
go.SetActive(false);
}
}
}
}
2024-04-16 15:39:29 -04:00
}
}