In this tutorial, we will learn to install, build, and deploy Unity ARCore apps for Android. Unity is a leading cross-platform game engine that is exceptionally easy to use for building game and graphic applications quickly.
Unity has developed something of a bad reputation in recent years due to its overuse in poor-quality games. It isn't because Unity can't produce high-quality games, it most certainly can. However, the ability to create games quickly often gets abused by developers seeking to release cheap games for profit.
This article is an excerpt from the book, Learn ARCore - Fundamentals of Google ARCore, written by Micheal Lanham.
The following is a summary of the topics we will cover in this article:
- Installing Unity and ARCore
- Building and deploying to Android
- Remote debugging
- Exploring the code
Installing Unity and ARCore
Installing the Unity editor is relatively straightforward. However, the version of Unity we will be using may still be in beta. Therefore, it is important that you pay special attention to the following instructions when installing Unity:
- Navigate a web browser to https://unity3d.com/unity/beta.
At the time of writing, we will use the most recent beta version of Unity since ARCore is also still in beta preview. Be sure to note the version you are downloading and installing. This will help in the event you have issues working with ARCore.
- Click on the Download installer button. This will download UnityDownloadAssistant.
- Launch UnityDownloadAssistant.
- Click on Next and then agree to the Terms of Service. Click on Next again.
- Select the components, as shown:
- Install Unity in a folder that identifies the version, as follows:
- Click on Next to download and install Unity. This can take a while, so get up, move around, and grab a beverage.
- Click on the Finish button and ensure that Unity is set to launch automatically. Let Unity launch and leave the window open. We will get back to it shortly.
Once Unity is installed, we want to download the ARCore SDK for Unity. This will be easy now that we have Git installed. Follow the given instructions to install the SDK:
- Open a shell or Command Prompt.
- Navigate to your Android folder. On Windows, use this:
cd C:Android
- Type and execute the following:
git clone https://github.com/google-ar/arcore-unity-sdk.git
- After the git command completes, you will see a new folder called arcore-unity-sdk.
If this is your first time using Unity, you will need to go online to
https://unity3d.com/ and create a Unity user account. The Unity editor will require that you log in on first use and from time to time.
Now that we have Unity and ARCore installed, it's time to open the sample project by implementing the following steps:
- If you closed the Unity window, launch the Unity editor. The path on Windows will be C:Unity 2017.3.0b8EditorUnity.exe. Feel free to create a shortcut with the version number in order to make it easier to launch the specific Unity version later.
- Switch to the Unity project window and click on the Open button.
- Select the Android/arcore-unity-sdk folder. This is the folder we used the git command to install the SDK to earlier, as shown in the following dialog:
- Click on the Select Folder button. This will launch the editor and load the project.
- Open the Assets/GoogleARCore/HelloARExample/Scenes folder in the Project window, as shown in the following excerpt:
- Double-click on the HelloAR scene, as shown in the Project window and in the preceding screenshot. This will load our AR scene into Unity.
At any point, if you see red console or error messages in the bottom status bar, this likely means you have a version conflict. You will likely need to install a different version of Unity.
Now that we have Unity and ARCore installed, we will build the project and deploy the app to an Android device in the next section.
Building and deploying to Android
With most Unity development, we could just run our scene in the editor for testing. Unfortunately, when developing ARCore applications, we need to deploy the app to a device for testing. Fortunately, the project we are opening should already be configured for the most part. So, let's get started by following the steps in the next exercise:
- Open up the Unity editor to the sample ARCore project and open the HelloAR scene. If you left Unity open from the last exercise, just ignore this step.
- Connect your device via USB.
- From the menu, select File | Build Settings. Confirm that the settings match the following dialog:
- Confirm that the HelloAR scene is added to the build. If the scene is missing, click on the Add Open Scenes button to add it.
- Click on Build and Run. Be patient, first-time builds can take a while.
- After the app gets pushed to the device, feel free to test it, as you did with the Android version.
Great! Now we have a Unity version of the sample ARCore project running. In the next section, we will look at remotely debugging our app.
Remote debugging
Having to connect a USB all the time to push an app is inconvenient. Not to mention that, if we wanted to do any debugging, we would need to maintain a physical USB connection to our development machine at all times. Fortunately, there is a way to connect our Android device via Wi-Fi to our development machine. Use the following steps to establish a Wi-Fi connection:
- Ensure that a device is connected via USB.
- Open Command Prompt or shell.
On Windows, we will add C:Androidsdkplatform-tools to the path just for the prompt we are working on. It is recommended that you add this path to your environment variables. Google it if you are unsure of what this means.
- Enter the following commands:
//WINDOWS ONLY
path C:Androidsdkplatform-tools
//FOR ALL
adb devices
adb tcpip 5555
- If it worked, you will see restarting in TCP mode port: 5555. If you encounter an error, disconnect and reconnect the device.
- Disconnect your device.
- Locate the IP address of your device by doing as follows:
- Open your phone and go to Settings and then About phone.
- Tap on Status. Note down the IP address.
- Go back to your shell or Command Prompt and enter the following:
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
adb connect [IP Address]
- Ensure that you use the IP Address you wrote down from your device.
- You should see connected to [IP Address]:5555. If you encounter a problem, just run through the steps again.
Testing the connection
Now that we have a remote connection to our device, we should test it to ensure that it works. Let's test our connection by doing the following:
- Open up Unity to the sample AR project.
- Expand the Canvas object in the Hierarchy window until you see the SearchingText object and select it, just as shown in the following excerpt:
Hierarchy window showing the selected SearchingText object
- Direct your attention to the Inspector window, on the right-hand side by default. Scroll down in the window until you see the text "Searching for surfaces…".
- Modify the text to read "Searching for ARCore surfaces…", just as we did in the last chapter for Android.
- From the menu, select File | Build and Run.
- Open your device and test your app.
Remotely debugging a running app
Now, building and pushing an app to your device this way will take longer, but it is far more convenient. Next, let's look at how we can debug a running app remotely by performing the following steps:
- Go back to your shell or Command Prompt.
- Enter the following command:
adb logcat
- You will see a stream of logs covering the screen, which is not something very useful.
- Enter Ctrl + C (command + C on Mac) to kill the process.
- Enter the following command:
//ON WINDOWS
C:Androidsdktoolsmonitor.bat
//ON LINUX/MAC
cd android-sdk/tools/
monitor
- This will open Android Device Monitor. You should see your device on the list to the left. Ensure that you select it. You will see the log output start streaming in the LogCat window. Drag the LogCat window so that it is a tab in the main window, as illustrated:
Android Device Monitor showing the LogCat window
- Leave the Android Device Monitor window open and running. We will come back to it later.
Now we can build, deploy, and debug remotely. This will give us plenty of flexibility later when we want to become more mobile. Of course, the remote connection we put in place with adb will also work with Android Studio. Yet, we still are not actually tracking any log output. We will output some log messages in the next section.
Exploring the code
Unlike Android, we were able to easily modify our Unity app right in the editor without writing code. In fact, given the right Unity extensions, you can make a working game in Unity without any code. However, for us, we want to get into the nitty-gritty details of ARCore, and that will require writing some code. Jump back to the Unity editor, and let's look at how we can modify some code by implementing the following exercise:
- From the Hierarchy window, select the ExampleController object. This will pull up the object in the Inspector window.
- Select the Gear icon beside Hello AR Controller (Script) and from the context menu, select Edit Script, as in the following excerpt:
- This will open your script editor and load the script, by default, MonoDevelop.
Unity supports a number of Integrated Development Environments (IDEs) for writing C# scripts. Some popular options are Visual Studio 2015-2017 (Windows), VS Code (All), JetBrains Rider (Mac), and even Notepad++(All). Do yourself a favor and try one of the options listed for your OS.
- Scroll down in the script until you see the following block of code:
public void Update ()
{
_QuitOnConnectionErrors();
- After the _QuitOnConnectionErrors(); line of code, add the following code:
Debug.Log("Unity Update Method");
- Save the file and then go back to Unity. Unity will automatically recompile the file. If you made any errors, you will see red error messages in the status bar or console.
- From the menu, select File | Build and Run. As long as your device is still connected via TCP/IP, this will work. If your connection broke, just go back to the previous section and reset it.
- Run the app on the device.
- Direct your attention to Android Device Monitor and see whether you can spot those log messages.
Unity Update method
The Unity Update method is a special method that runs before/during a frame update or render. For your typical game running at 60 frames per second, this means that the Update method will be called 60 times per second as well, so you should be seeing lots of messages tagged as Unity. You can filter these messages by doing the following:
- Jump to the Android Device Monitor window.
- Click on the green plus button in the Saved Filters panel, as shown in the following excerpt:
Adding a new tag filter
- Create a new filter by entering a Filter Name (use Unity) and by Log Tag (use Unity), as shown in the preceding screenshot.
- Click on OK to add the filter.
- Select the new Unity filter. You will now see a list of filtered messages specific to Unity platform when the app is running on the device. If you are not seeing any messages, check your connection and try to rebuild. Ensure that you saved your edited code file in MonoDevelop as well.
Good job. We now have a working Unity set up with remote build and debug support.
In this post, we installed Unity and the ARCore SDK for Unity. We then took a slight diversion by setting up a remote build and debug connection to our device using TCP/IP over Wi-Fi. Next, we tested out our ability to modify the C# script in Unity by adding some debug log output. Finally, we tested our code changes using the Android Device Monitor tool to filter and track log messages from the Unity app deployed to the device.
To know how to setup web development with JavaScript in ARCore and look through the various sample ARCore templates, check out the book Learn ARCore - Fundamentals of Google ARCore.
Getting started with building an ARCore application for Android
Unity plugins for augmented reality application development
Types of Augmented Reality targets