If you have ever thought about an augmented reality app on mobile devices that sparked your genuine interest to start developing your own AR-based Android app, then this article is a great place to learn basic beginner skills.
Once we've installed everything we need, we'll let's move on to creating an AR app that will allow us to explore surfaces and place custom objects on these surfaces.
Make sure you have a compatible phone
Before you go too far, remember that you will need a compatible device in order to test your application. Luckily, when ARCore left preview and moved to release, Google released a huge number of ARCore-compatible devices.
Download all software
Now, let's move on to the software we need. Here's what you'll need:
- Download Unity 2017.3 (to work with the first official release of ARCore "2017.3" or higher required);
- Download and install Android Studio 3.0 or higher since this is the easiest way to get the Android SDK installed. And make sure the SDK version you chose during the installation process is Android SDK version 7.0 (API level 24) or higher;
- Download "arcore-unity-sdk-v1.0.0.unitypackage.";
- Download sample resources arcore-intro.unitypackage;
- Finally, download the free billboard model from TurboSquid. When prompted with this option, download the "DAE" version of Collada
When installing Unity, we need to make sure we have the settings that will be used for ARCore development installed during this process.
Go to the location where you downloaded all the files for this project and double-click the "UnityDownloadAssistant-2017.3" file.
Move forward through the installation process until you reach the selection window components. Now make sure "Unity 2017.3.1f1" (or whichever version of 2017.3 you install) is selected, as well as "Android build support."
- If you are a frequent user of Unity, with several different versions installed, you may want to use a folder other than the default. Also, you can use a name other than just "Unity". In any case, once the decision has been made and the folder name has been added to the Unity installation folder field, click the "Next" button and the installation process will begin.
- If this is your first time using Unity, launch the application and create a new login account.
Create your ARCore project
Now we'll create a new project and set up the ARCore specifics we need to get our project up and running.
- Launch the Unity application and click on the "Create" button. Name the new project ARCore and then click the "Create Project" button.
- Once the main Unity window opens, press Ctrl + Shift + B or click the File button, then Build Options. From there, click on the "Android" option under Platform and then click on the "Switch Platform" button. When the Unity logo appears next to the Android option, you'll know it's selected. Click on the "player settings" button to continue.
- On the "General" tab, under the "Other Settings" heading, click on the "Multithreaded Rendering" checkbox to make sure it's not selected.
- In the "Package name" field, enter a name that must be unique for your device, as this can cause problems if it has the same package name as another application.
- Now we need to install the lowest compatible Android OS version that is compatible with ARCore. Find the "minimum API level" option, click on its drop-down menu and select "Android 7.0 'Nougat'"
- Click on the "XR Settings" button at the bottom of the inspector window. Check the box "supported by ARCore" to make sure it's selected, then close these settings.
Adding ARCore SDK & Assets
Finally, to finish, we need to import our ARKit elements into our scene. Double click on the "arcore-unity-sdk-v1.0.0." file "unitypackage".
This will bring up the Unity Package Import window. Just click on the "Import" button. We don't need to undo anything in this list, just leave it as it is.
Create a new scene
We need to create our scene, but we also need a place to save it. So let's create a folder for our scene. In the project window, right click on "Assets", then select "New" and then "Folder". Name the new folder Scenes.
Next, click on "File" in the menu bar and then "Save Scene as". Navigate to your newly created "scenes" folder. Enter NRARCore (or any other name that will help you remember the scene) in the Filename field and click the "Save" button.
Drag ARCore Prefabs into the scene
Prefabs are an asset type in Unity that allow us to store a collection of GameObjects and their associated components in a way that is easy to create in the engine in real time.
All settings saved when creating a prefab are saved with the components themselves.
In Unity, in the hierarchy window, select both "main camera" and "directional light". Right-click on them and select "Delete" when the menu appears.
In the PROJECT window Navigate to the "Prefabs" directory under "GoogleARCore". "Select prefabs" device "ARCore" and "environmental light", then drag a pair of prefs from the project window to the hierarchy window.
Then click on the New button in the hierarchy window, select "user interface" and then "event system."
Create a C# Class
To start this part of the tutorial, we need a place to put our script files, so we'll create a scripts folder. In the project window, with the "assets" folder selected, right-click in an open space and select "New" and then "Folder".
Name this folder when prompted.
Now double click on the newly created folder to open it. Right-click again in the open space and select "New" and then "C# Script."
Name the file "AppController" and double-click it to open it in our IDE of choice.
Here, in our using block, at the top of the class (line 2), we need to add the line: using GoogleARCore;
This statement allows us to access elements of the Google ARCore SDK.
On line 4 we have a class declaration. While nothing needs to be changed here, it's worth noting that most Unity classes will derive from the MonoBehaviour class. This base class is what adds most of the Unity specific functionality to any objects we create.
When Unity creates a C# class, it automatically creates two methods: Start() and Update(). In our Start() method, we need to add a method call: QuitOnConnectionErrors();
The purpose of the QuitOnConnectionErrors() method is to request and receive permission from the device and user to access the camera. If for some reason permission cannot be given, the application will close.
In the Update() method, we need to add lines 14-20 in the image below.
A frame is a collection of information about the current state of the device, including the position and rotation of the camera in relation to the surrounding world, light estimation information, tracking state, and objects in the scene.
Here we check to see if the state of the current frame is actually being tracked. If it's not, we set the screen timeout to 15 seconds. This gives the tracking some time to recover before shutting down.
If TrackingState is tracking, then sleepTimeout is set to NeverSleep.
In the QuitOnCeonnectionErrors() method, we are testing for three conditions. When any of these conditions are met, we send the appropriate on-screen error message using the ShowAndroidToastMessage() method.
Configuring the device for testing
In order to get our Android package file (or APK file) on the phone and test it, we need to enable "developer options". This menu is hidden by default, but can be easily unlocked by tapping the build number in the About phone section of the Settings app seven times in a row. You may need to enter your PIN to complete.
In Developer Options, make sure USB Debugging is enabled.
Go back to the main page of the settings menu, select Lock screen and security or something similar. Once inside this screen, select "unknown sources" and enable this option. If you don't see this option, it's probably because you're using Android Oreo or Android P. In this case, it will ask you to enable "Unknown Sources" when you try to install the APK.
After all the necessary settings, you can proceed to testing and fixing bugs in your Android AR game.