Skip to content

PietroPaziniPassos/AR_ImageTracking_ProjectSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity AR Image Tracking Project Sample

Overview

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.

How to get started

  1. Clone this repository on Unity;
  2. Open the Main scene;
  3. Change the plataform for Android;
  4. Import the images that you want to track inside Textures folder;
  5. Add them to Reference Image Library in Textures folder;
  6. Import the 3D objects inside 3D folder;
  7. Add them to Ar Objects in Place Content Component attached in XR Origin;
  8. Generate a build for your android;
  9. Be happy!

About the code

Librarys

Main functionality

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors