Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Build an ARCore app with Unity from scratch

Save for later
  • 11 min read
  • 21 May 2018

article-image

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:

  1. 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.

  1. Click on the Download installer button. This will download UnityDownloadAssistant.
  2. Launch UnityDownloadAssistant.
  3. Click on Next and then agree to the Terms of Service. Click on Next again.
  4. Select the components, as shown:

unity-arcore-application-android-img-0

  1. Install Unity in a folder that identifies the version, as follows:

unity-arcore-application-android-img-1

  1. Click on Next to download and install Unity. This can take a while, so get up, move around, and grab a beverage.
  2. 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:

  1. Open a shell or Command Prompt.
  2. Navigate to your Android folder. On Windows, use this:

cd C:Android

  1. Type and execute the following:

git clone https://github.com/google-ar/arcore-unity-sdk.git

  1. 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:

  1. 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.
  2. Switch to the Unity project window and click on the Open button.
  3. 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:

unity-arcore-application-android-img-2

  1. Click on the Select Folder button. This will launch the editor and load the project.
  2. Open the Assets/GoogleARCore/HelloARExample/Scenes folder in the Project window, as shown in the following excerpt:

unity-arcore-application-android-img-3

  1. 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:

  1. 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.
  2. Connect your device via USB.
  3. From the menu, select File | Build Settings. Confirm that the settings match the following dialog:

unity-arcore-application-android-img-4

  1. 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.
  2. Click on Build and Run. Be patient, first-time builds can take a while.
  3. 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:

  1. Ensure that a device is connected via USB.
  2. 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.

  1. Enter the following commands:

//WINDOWS ONLY
path C:Androidsdkplatform-tools

//FOR ALL 
adb devices
adb tcpip 5555

  1. If it worked, you will see restarting in TCP mode port: 5555. If you encounter an error, disconnect and reconnect the device.
  2. Disconnect your device.
  3. Locate the IP address of your device by doing as follows:
    1. Open your phone and go to Settings and then About phone.
    2. Tap on StatusNote down the IP address.

  1. Go back to your shell or Command Prompt and enter the following:
  2. 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 $19.99/month. Cancel anytime

adb connect [IP Address]

  1. Ensure that you use the IP Address you wrote down from your device.
  2. 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:

  1. Open up Unity to the sample AR project.
  2. Expand the Canvas object in the Hierarchy window until you see the SearchingText object and select it, just as shown in the following excerpt:

unity-arcore-application-android-img-5

Hierarchy window showing the selected SearchingText object

  1. 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…".

  1. Modify the text to read "Searching for ARCore surfaces…", just as we did in the last chapter for Android.
  2. From the menu, select File | Build and Run.
  3. 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:

  1. Go back to your shell or Command Prompt.
  2. Enter the following command:

adb logcat

  1. You will see a stream of logs covering the screen, which is not something very useful.
  2. Enter Ctrl + C (command + C on Mac) to kill the process.
  3. Enter the following command:

//ON WINDOWS
C:Androidsdktoolsmonitor.bat

//ON LINUX/MAC
cd android-sdk/tools/
monitor

  1. 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:

unity-arcore-application-android-img-6

Android Device Monitor showing the LogCat window

  1. 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:

  1. From the Hierarchy window, select the ExampleController object. This will pull up the object in the Inspector window.
  2. Select the Gear icon beside Hello AR Controller (Script) and from the context menu, select Edit Script, as in the following excerpt:

unity-arcore-application-android-img-7

  1. 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.


 

  1. Scroll down in the script until you see the following block of code:

public void Update ()
{
    _QuitOnConnectionErrors();

  1. After the _QuitOnConnectionErrors(); line of code, add the following code:

Debug.Log("Unity Update Method");

  1. 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.
  2. 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.
  3. Run the app on the device.
  4. 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:

  1. Jump to the Android Device Monitor window.
  2. Click on the green plus button in the Saved Filters panel, as shown in the following excerpt:

unity-arcore-application-android-img-8

Adding a new tag filter

  1. Create a new filter by entering a Filter Name (use Unity) and by Log Tag (use Unity), as shown in the preceding screenshot.
  2. Click on OK to add the filter.
  3. 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