This repository is an augmented reality Unity project based on image tracking. The objective is to facilitate the construction of applications of this model. This project was built for the Android platform.
- Clone this repository on Unity;
- Open the Main scene;
- Change the plataform for Android;
- Import the images that you want to track inside Textures folder;
- Add them to Reference Image Library in Textures folder;
- Import the 3D objects inside 3D folder;
- Add them to Ar Objects in Place Content Component attached in XR Origin;
- Generate a build for your android;
- Be happy!
using UnityEngine.XR.ARSubsystems;
public class PlaceContent : MonoBehaviour
{
[SerializeField]
public bool followImage;
[SerializeField]
private List<GameObject> ArObjects = new List<GameObject>();
private readonly Dictionary<string, GameObject> instantiatedObjects = new Dictionary<string, GameObject>();
private ARTrackedImageManager arTrackedImageManager;
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) {
foreach (var trackedImage in eventArgs.added) {
foreach (GameObject currentObject in ArObjects) {
if(string.Compare(trackedImage.referenceImage.name, currentObject.name, StringComparison.OrdinalIgnoreCase) == 0 && !instantiatedObjects.ContainsKey(currentObject.name)) {
GameObject instantiatedObject = Instantiate(currentObject, trackedImage.transform);
instantiatedObjects[currentObject.name] = instantiatedObject;
}
}
}
if(followImage) {
foreach (var trackedImage in eventArgs.updated) {
instantiatedObjects[trackedImage.referenceImage.name].SetActive(trackedImage.trackingState == TrackingState.Tracking);
}
foreach (var trackedImage in eventArgs.removed) {
Destroy(instantiatedObjects[trackedImage.referenceImage.name]);
instantiatedObjects.Remove(trackedImage.referenceImage.name);
}
}
}
}This code shows how the 3D object is generated and tracks the movement of the image. It is also possible to observe that the 3D object corresponding to the image is identified by its name. If the name of the object is the same as the name of the image, it will be generated, otherwise it will not be.
Made by Pietro Pazini