-
Notifications
You must be signed in to change notification settings - Fork 0
The "Hello ARC" App
This "Hello World" guide walks you through creating a project, installing the ARC library, and creating the basic components for your first networked game.
This tutorial requires a Unity version that supports the Multiplayer HLAPI, the AR Foundation package, and the ARCore or ARKit XR plugins, as well as an AR-capable mobile device.
- Open the Unity Hub.
- Click New.
- Select type "3D".
- Rename the project "Hello World".
- Select the location to save the project.
See the documentation linked above to install HLAPI, AR Foundation and its XR plugins, as well as configuring your project for a mobile AR app.
See the Importing the Library guide to install the ARC library.
First, we must create a basic AR scene for the project. According to the Scene Setup manual:
- Right-click in the scene hierarchy.
- Select XR > AR Session and XR > AR Session Origin from the context menu.
- Select the AR Camera game object in the scene hierarchy, and set its tag to
MainCamera
.
Your project hierarchy should now look like this:
In this section, we will create the basic building blocks of a multiuser shared session.
In this section, we will add an ARC Manager.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename the
GameObject
ARC Manager.
There should only be one ARC Manager in your scene. It is the object that contains the
ARCSetupManager
component. You may get unexpected results if you create more than one ARC Manager.
- Select ARC Manager.
- Click Add Component in the Inspector Tab.
- Select ARC from the list shown.
- Select the
ARCSetupManager
component from the list displayed. - Save your scene.
You may have noticed that when adding the ARCSetupManager
component, a CaptainsMess
component is added as well. This is because the ARC library requires it to manage networking. We will now set it up with a user Prefab and a network event listener.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename it ARC User.
- Add an
ARCUser
component. - Click the Assets folder.
- Create a new folder and call it Prefabs.
- Make ARC User a prefab by dragging it to the Prefabs folder you just created.
- Delete ARC User from the scene.
We remove User because we will be using CaptainsMess to spawn the user. The library cannot track objects that start in the scene.
- Select ARC Manager.
- Inside the
CaptainsMess
component tab, locate thePlayer Prefabs
field. - Drag the user prefab from above into the empty slot.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename it ARC Listener.
- Add an
ARCSessionListener
component. - Select ARC Manager.
- Inside the
CaptainsMess
component tab, locate theListener
field. - Drag the ARC Listener game object from the Hierarchy into the empty slot.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename it ARC Session.
- Add an
ARCSession
component. - Click the Assets folder.
- Make ARC Session a prefab by dragging it to the Prefabs folder.
- Delete ARC Session from the scene.
- Select ARC Listener.
- Inside the
ARCSessionListener
component tab, locate theShared Session Prefab
field. - Drag the ARC Session prefab from above into the empty slot.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename it Scenery and centre it at
(0, 0, 0)
. - Create some 3D objects and place them around the scene.
- Select all 3D objects above and parent them to the Scenery object.
- Click the Assets folder.
- Make Scenery a prefab by dragging it to the Prefabs folder.
- Delete Scenery from the scene.
- Select ARC Manager.
- Inside the
ARCSetupManager
component tab, locate theStatic Scene Prefab
field. - Drag the Scenery prefab from above into the empty slot.
The current object setup is enough for a simple scene. Since the ARC library needs user input to work, we will create a user interface to interact with it. While this mainly consists of GUI elements, the ARCSetupManager
component exposes some meaningful fields too.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select UI > Button. A Canvas object with a
Canvas
component will be created automatically. - Rename the button
GameObject
ButtonStart. - Expand the ButtonStart hierarchy, and select the nested Text
GameObject
. - Inside the
Text
component tab, locate theText
field and change its content to "Start". - Create three more buttons, rename them ButtonCancel, ButtonCapture, and ButtonContinue respectively, and change their description text accordingly.
- Set all buttons inactive except for ButtonStart.
- Select ButtonStart in the Hierarchy tab of the Main Unity Window.
- Inside the
Button
component tab, locate theOn Click()
Event field. - Click
+
to create a slot. - Select ARC Manager and drag it in the empty
Object
field. - Click "No Function".
- Select CaptainsMess > AutoConnect ().
We are adding a listener to the button's
OnClick
event. When the button is clicked (or tapped), theCaptainsMess.AutoConnect()
function will be triggered.
- Repeat steps 1-6 for the other three buttons:
-
ButtonCancel: add the
CaptainsMess.Cancel()
function. -
ButtonCapture: add the
ARCSetupManager.CaptureImage()
function. -
ButtonContinue: add the
ARCSetupManager.SubmitCapturedImages()
function.
-
ButtonCancel: add the
In this section, we will use the ARCSessionListener
events to control the UI visibility. This way, the user will see only buttons related to the current app state.
- Select ARC Listener in the Hierarchy tab of the Main Unity Window.
- Inside the
ARCSessionListener
component tab, locate theStart Connecting ()
field. - Add a slot for ButtonStart and assign the
GameObject.SetActive
function with argumentfalse
. - Add a slot for ButtonCancel and assign the
GameObject.SetActive
function with argumenttrue
.
We are adding more listeners to the events. This time, they are invoked by another script rather than being triggered by clicking a button.
- Inside the
ARCSessionListener
component tab, locate theStop Connecting ()
field. - Add a slot for ButtonStart and assign the
GameObject.SetActive
function with argumenttrue
. - Add a slot for ButtonCancel and assign the
GameObject.SetActive
function with argumentfalse
.
- Inside the
ARCSessionListener
component tab, locate theJoined Lobby ()
field. - Add a slot for ButtonStart and assign the
GameObject.SetActive
function with argumentfalse
. - Add a slot for ButtonCancel and assign the
GameObject.SetActive
function with argumentfalse
.
- Inside the
ARCSessionListener
component tab, locate theServer Created ()
field. - Add a slot for ButtonCapture and assign the
GameObject.SetActive
function with argumenttrue
. - Add a slot for ButtonContinue and assign the
GameObject.SetActive
function with argumenttrue
.
We want to show some buttons only to the current session host since they're the only ones who should capture the marker images for the current session.
- Inside the
ARCSessionListener
component tab, locate theLeft Lobby ()
field. - Add a slot for ButtonStart and assign the
GameObject.SetActive
function with argumenttrue
. - Add a slot for ButtonCancel and assign the
GameObject.SetActive
function with argumentfalse
. - Add a slot for ButtonCapture and assign the
GameObject.SetActive
function with argumentfalse
. - Add a slot for ButtonContinue and assign the
GameObject.SetActive
function with argumentfalse
.
- Select ARC Manager in the Hierarchy tab of the Main Unity Window.
- Inside the
ARCSessionManager
component tab, locate theCapture Completed ()
field inside theCapture Events
dropdown. - Add a slot for ButtonCapture and assign the
GameObject.SetActive
function with argumentfalse
. - Add a slot for ButtonContinue and assign the
GameObject.SetActive
function with argumentfalse
.
- Right-click in the Hierarchy tab of the Main Unity Window.
- Select 3D Object > Sphere.
- Rename the new
GameObject
GizmoScanning. - Set its scale to
(0.1, 0.1, 0.1)
. - Select 3D Object > Cube.
- Rename the new
GameObject
GizmoScanned. - Set its scale to
(0.1, 0.1, 0.1)
. - Click the Assets folder.
- Make GizmoScanning and GizmoScanned prefabs by dragging them to the Prefabs folder.
- Delete GizmoScanning and GizmoScanned from the scene.
- Select ARC Manager.
- Inside the
ARCSetupManager
component tab, locate theScanning Prefab
field. - Drag the GizmoScanning prefab from above into the empty slot.
- Inside the
ARCSetupManager
component tab, locate theMarker Scanned Prefab
field. - Drag the GizmoScanned prefab from above into the empty slot.
Now we will test to see if everything works as expected. Build the app and deploy it to your mobile device.
- When the app starts, look around you to let AR Foundation detect your surroundings.
- When you're done, tap Start.
If you now tap Cancel, the session startup process will stop, and the button Start should show up again.
- Take a picture of (at least) two flat objects on a horizontal surface by tapping on Capture.
- When you're done, move away. Then, tap Continue.
- Now, the scanning phase begins. Hover your phone over the objects you just photographed. Let AR Foundation detect the surface.
When an image is being tracked, a small 3D sphere will appear on top of it. Once an image has been scanned, a cube will replace the sphere. When all images are detected, the cubes will disappear, and the static scenery will appear instead.
- If you follow the previous steps using a different device, the capture button will not show up. Instead, the app will skip to the scanning phase once the host confirms the marker images to be used.
All connected devices must scan the markers, then everyone will see the same static scenery at the same spot in the environment.
Congratulations! You have created your first multiuser AR app! For the next steps, see Building on Hello ARC.