Build ARKit Application with Unity

David Guan
David Guan
Published in
4 min readOct 16, 2017

--

As a developer who got interested in 3D development lately, I’m very excited about ARKit for its wide availability and the data it provides.

Copy from WWDC17 Introducing ARKit slide
Lots of people have made and still making creative things powered by ARKit, and we can also be one of them.
The ARKit DEMO I created

Let’s get started

Create a new Unity project(or use your existing one), search and import the Unity ARKit plugin in the asset store.

Don’t forget to save and rename the scene :)

Unity ARKit plugin is 1.0.10 when this article is being written; I’ll update this article when there are breaking changes related to its usage.
The plugin contains a handy tool ARKitRemote which allows us developing AR app purely in the editor(watch this video for more about it), it also includes lots of examples, please check them after you have an overview of how ARKit works.

Rendering a cube in real world

Scripts under directory UnityARKit/NativeInterface, are the Unity’s scripting API implementations correspond to the ARKit native interface.
To reduce our work, the plugin’s authors also provide some boilerplate code under UnityARKit/Helpers and several useful materials.
Now, let’s continue with search some asset files and assign them to related components to achieve the goals listed below:

Blending 3D content to real-world
For the main camera, attach the Unity AR Video Script to it,
set its clear flag property to Depth Only and assign the YUVMaterial to Clear Material property.

Updating virtual camera’s position and orientation
Use the Unity AR Camera Manager Script and assign its Camera property to the main camera.
To achieve that, we can create an empty object(in this article, the name of that object is AppController, you can name it to whatever you want) and attach the script to it.

Now we can test whether the basic setup works.
Create a cube and drag it to make it a child of the main camera and make it always appear in front of the camera by setting the position.
Build the scene and run it in IOS device(uncheck the example one in build setting).

After running the application on your device, you will found the cube rendered in real-world.

User interactions

It provides nearly nothing with just rendering a cube, let’s create response when the user touches the screen(cube will appear in front of the touching point).

Drag the cube to Assets before deleting it so we can use it again later.

Let’s create a new script with the code below.

using UnityEngine;

public class AppController : MonoBehaviour {
public GameObject GameObjPrefab;

void Update () {
if (Input.touchCount > 0) {
var touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
// Put the cube 0.5 meter in front of the screen
var newObjectPosition = Camera.main.ScreenToWorldPoint(
new Vector3(touch.position.x, touch.position.y, 0.5f)
);
Instantiate (GameObjPrefab, newObjectPosition, Quaternion.identity);
}
}
}
}

Attach the newly created script to the AppController, also assign the GameObjPrefab property to the cube prefab.

Now, we can build and try the application again.
Below is the result after I touched the screen several times(sorry for the dark lighting).

Enable shadow in the direction light properties if you need it

What’s next?

I leave the adventures to you, some hints:
By using HitTest, we can put things above real-world planes(tables, ground) instead of letting them floating in the air(you can reference Examples/UnityARBallz).
By using Helpers/UnityARAmbient, we can capture the intensive of environment lighting, and adjust our application’s lights based on it to make 3D content more real.

There are much more to try(creating your first AR game, building your 3D models and use them in AR, etc..), I’ll keep updating my blogs and Youtube channel when I find more :)

Also found these two articles written by Matt Miesnieks very helpful, take a look when you have time!
AR-First Mobile second
Why is ARKit better than the alternatives?

Thanks for your reading!

--

--

David Guan
David Guan

I program machines to do web and graphics stuff, also write about them.