Setup AR Foundation in Unity
Objective: Setup Unity to detect and display augmented image.
With the new Unity Hub 3.0.1, there is an option create a new project using AR Core. This is very nice because it will add the necessary packages and objects for AR Foundation.
A prompt will ask you select your device and addons that you will use. For example, go to Unity File > Build Settings and select Android and switch if not already the selected build. Add the open scene to the build.
Select the Player Settings then in lower left choose XR Plug-in Management. Click AR Core on the Android tab, and close player and build settings windows.
In the hierarchy window select the AR Session Origin to open the inspector window.
Add AR Tracked Image Manager script to the AR Session Origin. The Serialized Library And Tracked Image Prefab need to be populated.
Create a Cube and set it up as shown below, noting that this AR cube is one tenth scale. Drag it into the Assets > Prefabs folder to create a prefab and delete the original Cube in the hierarchy window. Drag the newly created prefab into the Tracked Image Prefab slot within the AR Tracked Image Manager above.
Create an Image folder within the project’s Assets folder. Right click on the Image folder and create an XR > ReferenceImageLibrary. Select this library which is initially empty and add reference images. For this article images one.png and two.png from https://github.com/Unity-Technologies/arfoundation-demos/tree/master/Assets/ImageTracking/TrackedImages were used. Drag the new ReferenceImageLibrary into the Serialized Library field in AR Tracked Image Manager.
Create a Scripts folder and add an ImageRecognition script as shown below. This script assigns the _arTrackedImageManager that was earlier placed on the AR Session Origin, detecting reference images. If a reference image is detected the OnImageChange method is added to _arTrackedImageManager.trackedImageChanged. This requires ARTrackedImagesChangedArgs args from ARFoundation namespace as an input for the OnImageChange method. If the reference image is lost the OnImageChange is removed.
Go to Unity File > Build Settings and deploy to Android then run app. If the the phone detects the reference image, either printed or on a monitor, a cube should appear in front of the image.
If during building you get the errors shown below then the project contains a reference image library which is empty. Confirm that all reference libraries have images added or delete empty libraries.