Previously we saw how to set up our Unity project for AR and Android deployment. In this blog, we’ll learn to place an object on top of a detected image using the image tracking feature of AR Foundation. We’ll also learn about its capabilities and the best practices while choosing an image to get the best result.
Make sure to have the following requirements met before proceeding:
Let’s start by setting up our scene for image tracking
The component requires us to add a reference image library and a tracked image prefab. But before we can add it, we’ll have to create it first. So, let’s see how that’s done.
Creating a reference image library is simple, but we’ll have to download and import images into our project before we do that.
Now to create the reference image library:
With that, we have created the reference library. Now, it’s time to create a prefab that will be tracked when the image is detected.
The tracked image prefab can be any 3D or 2D model that you’ll like to instantiate when the image gets detected. You can either download an existing one or create a model of your own. Here, we’ll create a Cube prefab which you can later replace with any other prefab of your choice.
Now that we have created the Image Library and Tracked Image Prefab, we can reference them to the AR Tracked Image Manager.
Let’s now reference the assets we created and test them out.
Once it’s built, the application will open automatically on your mobile device and you can test it. It should instantiate the cube when the Earth image is detected and track it as you move along.
Now, if you are wondering, how you can do something else like display text or play audio when the image is detected? Then, we got you covered. In the next section, we’ll write a script that will allow us to subscribe to the events from the AR Tracked Imager Manager component.
To do something else apart from placing an object on the detected image, we’ll have to write a script and subscribe to the events that are involved by the AR Tracked Image Manager component when the image gets detected.
So, create a new script by right-clicking in the Project window → select Create → C# Script, name it as ExampleImageDetection and copy the following code:
The code above:
So with that, you’ll now be able to do additional things apart from placing the prefab on top of the image.
When it comes to image tracking AR Core (the plugin used by AR Foundation) has the following capabilities:
Here are some of the best practices when it comes to choosing the images for tracking:
Now you know everything when it comes to image detection with AR Foundation on Android. It can be used to overlay menus on top of detected images to perform some action or it can be used to show the 3D model of the image, etc. There are many ways in which this feature can be used. We just need to be creative with it. Image tracking is just one of the many features AR Foundation has to offer. In the next blog, we’ll see how plane detection is done.
If you've enjoyed the insights shared here, why not spread the word? Share the post with your friends and colleagues who might also find it valuable.
Your support means the world to us and helps us create more content you'll love.