In this tutorial, we will cover how to get started with Unity and Vuforia.
The Unity version used in this tutorial is 2017.3.1f1. Just for your info, Vuforia is now integrated with the Unity editor without any external package since Unity 2017.2 as seen in this link. We will start first on Vuforia's website, then to Unity to set everything up. Finally, we will build it and test on Android.
Creating an Account on Vuforia
Visit Vuforia developers website and click on Register at the top right corner. If you already have an account Log In and skip to the next section. Login/Register Vuforia
The registration is straightforward nothing special. You enter your stuff and press enter. The only different thing is that you to verify your account, you receive Two emails. One containing the license agreement which is ignored anyway and one containing the activation link. If you didn't receive the activation link make sure you mail the support team until your account is verified. Registration
Create Application Key
After logging in, you will hopefully have the same screen as this. There are 2 main sections License Manager where you manage your apps and get your License Key and the Target Manager where you manage your Databases and add Image Targets and so on. By default, the landing screen is the License Manager. As you can see I already have an app called AR Test.
Click on Get Development Key button to create a new app. Give it a name, check the box where you lie about the terms and conditions unless you really know what you are doing and finally press the Confirm button. Get Development Key
You will find your newly creating application there at the top. Click it and copy the license key somewhere as we will use it in Unity. License key (I opened two different tabs rather than creating 2 different screenshots)
Now open the Target Manager, click on the Add Database button, give it a name and select Device then finally, click the Create button. Make sure that the name you enter doesn't contain spaces or some special characters. For the difference between these 3 choices, check Vuforia's Documentation Creating Database
Once created, click on the database name then Add Target button. I will be adding this image from Vuforia's Documentation on optimizing target detection which I recommend to read. The Width is just the actual width of the image in real life if you are making something with realistic scale. This can be changed later in Unity so it's ok to write it 1.
Add an Image Target
After adding an image, make sure that it has 5-Stars then download the database from Download Database (All) button. Make sure you choose the Unity Editor editor because it's not selected by default.
Stars and Download Database button Download Dialog
Right now, you should have 2 things with you. The license key and the .unitypackage of the database.
Create a new project and let's get ready! New Unity Project
Open the Build Settings by pressing Ctrl + Shift + B and switch the platform to Android. If you can't build for Android I recommend checking my other tutorial regarding Setting up Android SDK and JDK in Unity
Change platform to Android
In the Others section change the Package Name and disable Android TV Compatibility or else you will get some nice build errors.
Scroll down to the XR Settings and enable Vuforia Augmented Reality Supported. You will find a new folder named Resources appearing from the middle of nowhere. Don't worry, we will check it in the coming sections.
We will make few changes:
- Create a new folder for the scenes and save the scene
- Open the Build Settings and add the newly saved scene
- Delete the Main Camera
- Add the AR Camera as if you are creating a simple Cube. When asked to import the assets press Import.
Some changes in the Scene
Next, we will import the .unitypackage we downloaded earlier which contains the Database
Import the Database downloaded
To open the VuforiaConfiguration, go to the Resources folder created and select the only thing inside it and check the Inspector window or simply press Ctrl + Shift + V.
Now paste the license key you have been keeping for so long. Also, Check both the Load XX Database and the Activate where XX is the name of your downloaded database.
You can check other settings like the WebCam to be able to test in the editor without building.
More Scene Setting
- Create an Image Target from Vuforia>Image.
- Select the ImageTarget and in the Image Target Behavior (Script) change the Database to the one you downloaded.
More scene settings
Now anything child to this ImageTarget will appear once the camera detects the target when you build.
Let's add a cube for example. This cube can be replaced with anything you want. For the purpose of illustration, let's leave it as a cube.
Add a cube and make it child to the Image Target object. After that, Change the Transform component as follow
- Position (0, 0.2, 0)
- Rotation (0, 0, 0)
- Scale (0.3, 0.3, 0.3)
Build the APK
Simply, Go to the Build Settings and press Build.
We created an account, app license, and a database. Then we configured Unity and imported Vuforia. Finally, we added the image target and everything that's child to it will move with the target like the cube we added.
Screenshot from the app
- Vuforia Developer Portal
- Vuforia's Documentation - Comparison between Database types
- Vuforia's Documentation - Optimizing Target Detection
- Setting up Android SDK and JDK in Unity