AR Foundation in Unity: Getting Started
In this AR Foundation tutorial, you’ll learn how to build a cross-platform AR application in Unity. By Ken Lee.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
AR Foundation in Unity: Getting Started
20 mins
- Getting Started
- Understanding AR Foundation
- AR Foundation Architecture
- Building Your Application to the Device
- Installing AR Foundation Packages
- Setting Up the AR Environment
- Verifying AR Capabilities
- About ARSession and ARSessionOrigin
- ARSession
- ARSessionOrigin
- Exploring the Doodle Logic
- Bringing the Doddle Logic to AR
- Detecting the AR Surface
- Adding the AR Raycasting Logic
- Building the User Interface for AR
- Improving Visuals With Post Processing
- Where to Go From Here?
Verifying AR Capabilities
Luckily, you can use a basic feature called Feature Point Tracking to verify the AR capabilities. This feature finds the feature points in the AR environment.
- Select ARSessionOrigin in the Hierarchy, click Add Component in the Inspector, search for AR Point Cloud Manager and select the item.
- Then, right-click in the Hierarchy and select XR ▸ AR Default Point Cloud to create an AR Point Cloud object.
- When the object is created, assign it to AR Point Cloud Manager’s Point Cloud Prefab in the Inspector.
Now, build and run to check that the Point Cloud is working.
If you find lots of yellow spots, it means AR Foundation is working.
After testing, disable the ARPointCloudManager component to prevent feature points from showing up in the future.
If you’re building on Android, you may experience a problem related to the build setting.
You can fix it by changing the following items in the Android Player Setting:
- Remove Vulkan from the Graphics APIs.
- Set Minimum API Level to API Level 24.
About ARSession and ARSessionOrigin
In the last section, you used ARSession and ARSessionOrigin to make a simple AR application. Both of them are key components of AR Foundation programs, so it’s important to understand them.
ARSession
ARSession controls the lifecycle of an AR session. It keeps getting updates from the subsystem to check if the session is still alive.
ARSessionOrigin
ARSessionOrigin keeps your virtual objects in the correct position in AR environments.
It keeps getting updates from the subsystem and modifies the position of the virtual objects to stay aligned with the real environment. Notice that you need to place your AR objects under the ARSessionOrigin in the Hierarchy. If you don’t, the positions of the objects won’t update.
Exploring the Doodle Logic
Before making the AR version of the Doodling App, take a look at the original doodling logic.
The doodle logic consists of DoodlePen and DoodleLine.
DoodleLine
handles all the line-drawing logic. When users touch the screen, it casts a ray to find the position hit on the surface and feeds the position to Line Renderer to show the doodle line. Notice that the raycasting logic isn’t implemented in the DoodleLine class. The concrete implementation is defined outside, so we can toggle between the standard raycasting logic and AR raycasting logic later on.
DoodlePen
is a class for managing the doodle lines. When users touch the screen, a new DoddleLine object is instantiated with the selected line size and color. Currently, DoodlePen only supports non-AR mode. Your job is to add AR environment support with AR raycasting logic.
Consider developing non-AR logics first, because you can’t review your AR Scene in the Unity Editor. If you build your device to check your work, you’ll end up wasting a lot of time on the build process.
Bringing the Doddle Logic to AR
To evolve the Doodle app to AR, you need to do two things:
- Detect an AR Surface for doodling.
- Add the AR raycasting logic to DoodlePen.
Detecting the AR Surface
First, detect the AR surface:
- Open the Assets/RW/ARScene scene.
- Select ARSessionOrigin in the Hierarchy, click Add Component in the Inspector and find and select AR Plane Manager.
- Right-click an empty space in the Hierarchy and select XR ▸ AR Default Plane to create an AR Plane Object. This object is created when a surface is found in AR.
- Select AR Plane Manager in the Inspector and assign the AR Plane object to the AR Plane Manager Plane Prefab field.
Build and run. When you’re ready, scan a wall to find the AR surface.
You can now add the DoodlePen’s raycasting logic by modifying the DoodlePen code and component.
Adding the AR Raycasting Logic
In Assets/RW/Scripts, double-click DoodlePen.cs to launch the file in the code editor.
First, add two using statements above the class definition:
using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems;
Then, add the following variables inside the class definition:
public ARRaycastManager raycastManager; public bool arMode = false;
raycastManager
is used to reference ARRaycastManager in the scene, and arMode
is the flag to toggle between AR and non-AR mode.
Add the following method:
bool GetArRaycastLogic(out Vector3 hitPosition) { // 1 var hits = new List<ARRaycastHit>(); // 2 bool hasHit = raycastManager.Raycast(Input.mousePosition, hits, TrackableType.PlaneWithinInfinity); // 3 if (hasHit == false || hits.Count == 0) { hitPosition = Vector3.zero; return false; } else { hitPosition = hits[0].pose.position; return true; } }
Here’s how the code breaks down:
- Define the output variable for storing the hit information.
- Use the
ARRaycastManager
to find out where the AR surface is touched.Input.mousePosition
is the position where users touched the screen, andhits
contains the position where the ray is hitting the AR surface. - Return
true
and the hit position if the ray successfully hit a surface, or returnfalse
to the caller.
Lastly, modify the content of SetupRaycastLogic()
:
if (arMode) { doodleLine.raycastDelegate = GetArRaycastLogic; } else { doodleLine.raycastDelegate = GetNonArRaycastLogic; } doodleLine.gameObject.SetActive(true);
This lets the DoodleLine toggle between AR and non-AR raycasting logic depending on the arMode
variable.
Save the code and return to Unity.
Select ARSessionOrigin in the Hierarchy and add the AR Raycast Manager component from the Inspector.
Then, go to Assets/RW/Prefabs and drag DoodlePen into the ARSessionOrigin object in the Hierarchy. Select DoodlePen in the Hierarchy and set the following properties in the Inspector:
- Main Camera: AR Session Origin ▸ AR Camera
- Raycast Manager: AR Session Origin
- Ar Mode: check to enable
Build and run to test the AR doodling.
Building the User Interface for AR
Now you need to create the UI to control the color and size of the lines, along with a clear button to delete the drawn doodles.
You’ll also build a coaching dialog telling users to find a surface in the AR environment.
First, double-click DoodleUI.cs in Assets/RW/Scripts and add the following code above the class definition:
using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems;
Then, add the following variables inside the class definition:
public ARPlaneManager planeManager; public bool arMode;
Replace the code after SetPenDrawingBound()
in Start()
with the code below:
if (arMode) { // 1 SetDoodleUIVisible(false); SetCoachingUIVisible(true); // 2 planeManager.planesChanged += PlanesChanged; } else { // 3 SetDoodleUIVisible(true); SetCoachingUIVisible(false); }
Here’s how the code breaks down:
- If using AR mode, show the coaching dialog.
- Add the listener for the
planesChanged
event. - Show the Doodle UI if not using AR mode.
Now, add the PlanesChange
method:
private void PlanesChanged(ARPlanesChangedEventArgs planeEvent) { if (planeEvent.added.Count > 0 || planeEvent.updated.Count > 0) { SetDoodleUIVisible(true); SetCoachingUIVisible(false); planeManager.planesChanged -= PlanesChanged; } }
planeEvent.added.Count
and planeEvent.updated.Count
are examined to determine if a surface has been found. If the count is more than one, it means a surface has been found and the app can present the DoodleUI.
Save and return to the editor.
You still need to add and configure the DoodleUI.
Drag DoodleUI from Assets/RW/Prefabs to Canvas in the Hierarchy. Then, configure the properties of the DoodleUI script in the Inspector:
- Pen: DoodlePen
- Coaching UI: ScanSurfacePanel
- Doodle UI: DoodlePanel
- Plane Manager: AR Session Origin
- Ar Mode: check
Build and run.
Now you have more control over the style of the doodle lines.