0% found this document useful (0 votes)
6 views2 pages

AR Image Anchor Spawner Script

The document describes a Unity script for spawning prefabs based on tracked images using ARFoundation. It manages image tracking events to instantiate prefabs when images are detected and updates the UI to reflect changes. The script also handles the addition and removal of anchors associated with the tracked images.

Uploaded by

samuel.heirman37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

AR Image Anchor Spawner Script

The document describes a Unity script for spawning prefabs based on tracked images using ARFoundation. It manages image tracking events to instantiate prefabs when images are detected and updates the UI to reflect changes. The script also handles the addition and removal of anchors associated with the tracked images.

Uploaded by

samuel.heirman37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using UnityEngine;

using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

public class ImageAnchorSpawner : MonoBehaviour


{
public ARTrackedImageManager imageManager;
public ARAnchorManager anchorManager;
public GameObject instantiatedPrefab;
public GameObject instantiatedPrefab2;

public UIDocument documentDebug;


public Label labelDebug;

private Dictionary<string, GameObject> instantiatedPrefabs = new();

void Awake()
{
VisualElement root = [Link];
if (root != null)
{
labelDebug = root.Q<Label>("valid");
if (labelDebug != null)
{
[Link] = "Hello World";
}
}
}

void OnEnable()
{
[Link](OnTrackedImagesChanged);
}

void OnDisable()
{
[Link](OnTrackedImagesChanged);
}

async void OnTrackedImagesChanged(ARTrackablesChangedEventArgs<ARTrackedImage>


args)
{
// [Link] += "\nOnTrackedImagesChanged";
foreach (var trackedImage in [Link])
{
// [Link] += "\nAdded TrackedImage: " + [Link];
// [Link]("Added TrackedImage: " + [Link]);
await TrySpawnPrefab(trackedImage);
}

foreach (var trackedImage in [Link])


{
// [Link] += "\nUpdated TrackedImage: " + [Link];
// [Link]("Updated TrackedImage: " + [Link]);
await TrySpawnPrefab(trackedImage);
}
foreach (var trackedImage in [Link])
{
// [Link] += "\nRemoved TrackedImage: " + [Link];
// [Link]("Removed TrackedImage: " + [Link]);
}
}

async Task TrySpawnPrefab(ARTrackedImage trackedImage)


{

if ([Link] != [Link])
return;

if ([Link]([Link]))
return;

Pose pose = new Pose([Link],


[Link]);
var result = await [Link](pose);

if ([Link]())
{
var anchor = [Link];

GameObject prefab = Instantiate(instantiatedPrefab);


[Link]([Link], false);

// [Link] += $"\nPrefab spawned on image


'{[Link]}'";
[Link]([Link], prefab);
[Link] = false;
}
else
{
// [Link] += $"\nFailed to create anchor on image
'{[Link]}' : {[Link]}";
}
}
}

You might also like