Image Tracking with ARFoundation and Unity — Getting started

Raj Shirolkar
4 min readNov 19, 2020

--

Image tracking is one of the most handy features in ARFoundation. We can use this feature to keep augmented anything and everything under the sun based on finding a unique image in the environment. Let’s see how we can do this.

Prerequisites :

  • Unity 2020.2
  • Android device with ARCore support
  1. Create a Unity Project and go to build setting -> android -> switch platform to android.

2. Open the package manager and install the AR Foundation, ARCore XR Plugin and XR Plugin Management packages. Go to Player settings -> XR Plugin Management -> check the ARCore box.

3. Now go to player settings and remove the Vulkan Graphics API as it is not supported and change the Minimum API level to Android 7.0 ‘Nougat ’ (API level 24)

4. Now in the scene remove the Main Camera and add the AR Session Origin and AR Session objects.

5. In the ARSession object add the AR Tracked Image Manager script.

6. Now if you see, the AR Tracked Image manager script accepts a serialized library (we will check this out in depth in the next post) and a prefab to be instantiated when the image is detected. Now lets fill in these two by first creating a reference image library. Right click on assets -> create -> xr -> reference image library

7. Lets add an image to this reference library which we want to track in the environment. You can specify the size of the image if you want to but it’s not mandatory.

8. Now we will need a script to control the tracking of images. Lets make an ImageTracker.cs script which looks like

Attach this script to the ARSession Gameobject.

Explainer -

  • In this script whenever the gameobject this script is attached to is Awake() it will get the reference to the ARTRackedImageManager script we attached in step 5.
  • Whenever the object is enabled we fire start executing the OnImageChanged() function which loops through the event arguments and searches for our image using a foreach loop.
  • Once found we will simply log the name of the tracked image.
  • The OnDisable() method halts the tracking whenever the gameobject is disabled.

9. Create a simple cube gameobject and set its scale to a reasonable value like 0.15 metres since we’ll be augmenting it in real world dimensions. Convert that gameobject into a prefab by dragging the cube into the assets directory.

10. So far we have :

  • A serialized library containing the images we want to track
  • A Cube prefab which will be augmented on the image wherever the image is detected.
  • A ImageTracker.cs script attached to the ARSession Gameobject.

Lets add these things into the AR Tracked Image Manager component.

11. Now build and run the project and point the camera to the image you want to track and we’re done with the basics!! In the next post we will take a deeper dive into how we can gain better control of the gameobject position for real world projects.

--

--

Raj Shirolkar
Raj Shirolkar

Written by Raj Shirolkar

I make Augmented Reality apps and I love to solve problems!

Responses (1)