1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-23 23:58:24 -05:00
UDRIGEEKCup2024/Assets/Samples/MAVRIC.GEEKCup/QRCode/Scripts/TrackedImageChanged.cs

131 lines
4.5 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.Logging;
using Unity.Mathematics;
using UnityAtoms.BaseAtoms;
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 LogLevel logLevel = LogLevel.Warning;
[SerializeField] private ARTrackedImageManager trackedImageManager;
[SerializeField] private GameObjectReference parentObject;
[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))
{
Log.Error("Failed to find ARTrackedImageManager...", this.gameObject);
}
}
if (parentObject?.Value == null)
{
parentObject.Value = 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.transform.SetParent(parentObject.Value.transform);
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);
Log.Verbose("trackedImage.referenceImage.name: {0}", 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.transform.SetParent(parentObject.Value.transform);
goARObject.SetActive(true);
goARObject.transform.position = newPosition;
goARObject.transform.localScale = newScale;
goARObject.transform.localRotation = newRotation;
foreach (GameObject go in arObjects.Values)
{
Log.Verbose("Go in arObjects.Values: {0}", go.name);
if (go.name != name)
{
go.SetActive(false);
}
}
}
}
2024-04-16 15:39:29 -04:00
}
}