A fun way to NOMI
Stichting NOMI is a foundation that is committed to improving the lives of children that miss out on a normal life because they are required to visit the hospital regularly due to their chronic illness.
NOMI attempts to reach this goal by offering these children solutions such as interactive applications to find and interact with children with the same fate.
My contribution to this goal was to work with a team of programmers and Game Artists on a game for the mobile phone that uses augmented reality to take the player on a scavenger hunt within a certain location, usually a hospital. The scavenger hunt can be played by scanning signature NOMI branded images, each of which provides the player with a hint on what to do next. Some images then come with a minigame, which you need to complete to either move on or complete the scavenger hunt.
Augmented Reality was achieved in this project through the usage of the ARFoundation Toolkit within Unity.
The C# code is for an augmented reality (AR) image recognition application using Unity's ARFoundation. It initializes AR tracked images and manages their visibility and interactions. When an image is detected, it updates the position and state of associated prefabs, ensuring only one object is visible at a time. The code also handles enabling and disabling event listeners for image tracking changes.
Awake Method: Initializes the ARTrackedImageManager and instantiates the placeable prefabs at the start of the session.
private void Awake()
{
_arTrackedImageManager = FindObjectOfType();
foreach (GameObject prefab in _placeablePrefabs)
{
GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
newPrefab.name = prefab.name;
_spawnedPrefabs.Add(prefab.name, newPrefab);
newPrefab.SetActive(false);
}
}
OnEnable and OnDisable Methods: Handles subscribing and unsubscribing to the tracked images changed event.
private void OnEnable()
{
_arTrackedImageManager.trackedImagesChanged += OnImageChanged;
}
private void OnDisable()
{
_arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
}
OnImageChanged Method: Handles the responding to detected images and notifies events whenever an image is added, updated, or removed.
private void OnImageChanged(ARTrackedImagesChangedEventArgs args)
{
bool isTracked = false;
foreach (ARTrackedImage trackedImage in args.updated)
{
if (trackedImage.trackingState == TrackingState.Tracking)
{
isTracked = true;
}
UpdateImage(trackedImage);
}
_panel.ShowHidePanel(isTracked);
}
UpdateImage Method: Updates the position and visibility of the prefabs based on the tracked image.
private void UpdateImage(ARTrackedImage trackedImage)
{
string currentTrackedImageName = trackedImage.referenceImage.name;
Vector3 trackedImagePosition = trackedImage.transform.position;
TrackingState trackedImageState = trackedImage.trackingState;
GameObject prefab = _spawnedPrefabs[currentTrackedImageName];
prefab.transform.position = trackedImagePosition;
if (trackedImageState == TrackingState.Tracking && _previousTrackedImageName != currentTrackedImageName)
{
if (_previousTrackedImageName == null)
{
_previousTrackedImageName = currentTrackedImageName;
}
if (_spawnedPrefabs[_previousTrackedImageName] != null)
{
_spawnedPrefabs[_previousTrackedImageName].SetActive(false);
}
if (_spawnedPrefabs[currentTrackedImageName] != null)
{
_spawnedPrefabs[currentTrackedImageName].SetActive(true);
}
_previousTrackedImageName = currentTrackedImageName;
return;
}
if (trackedImageState == TrackingState.Tracking && _previousTrackedImageName == currentTrackedImageName)
{
_spawnedPrefabs[currentTrackedImageName].SetActive(true);
_tagManager.CheckCurrentTag(trackedImage);
return;
}
if (!_spawnedPrefabs[_previousTrackedImageName])
{
return;
}
_panel.ShowHidePanel(false);
}