Using Vuforia Engine with Unity as a Library (UaaL)

Unity introduced official support to include Unity as a library in native Android and iOS applications with Unity 2019.3.
Vuforia now supports this feature in Vuforia Engine 8.6. Please, follow the guide below to integrate Vuforia with Unity as a Library.

NOTE: this guide has been tested with Unity 2019.3.0f1.

Follow the Unity guides for UaaL setup from the links below, taking the additional Vuforia specific steps below into account.
We recommend running through the Unity guides before adding Vuforia Engine support to make sure your build setup works.

UaaL Android guide 
UaaL iOS guide

Project Setup (Android and iOS)  

To add Vuforia Engine support to the sample UnityProject from the guides above, install the “Vuforia Engine AR” package from Unity’s package manager. Make sure you are using Vuforia Engine 8.6 or later.

You may see some compiler errors related to tests. This is because the sample project provided by Unity misses a standard package. To add it back, add the following line to the Packages/manifest.json file:   "com.unity.ext.nunit": "1.0.0"

After that, no more errors should be seen in the log.

Now, let’s add some Vuforia functionality to the provided Unity project.

  • Open Scenes/SampleScene.
  • Add an ARCamera GameObject to the scene by selecting GameObject -> Vuforia Engine -> ARCamera.
  • Delete the MainCamera GameObject from the scene.
  • Add an image target by selecting GameObject -> Vuforia Engine -> Image.
    • Confirm the dialog asking you to import the default dataset.
  • Re-parent the Cube in the scene to the newly created image target and scale it down to match the width of the target.
  • Optional: Remove the Update()method from the Cube.cs script to stop the cube from rotating.

As a result of the changes, the cube displayed in the Unity scene will now be rendered on top of the Vuforia Astronaut image target.

Deinitializing Vuforia

Before moving on to the platform specific steps, it is necessary to add the following function call to the unload button in both Unity and your native platform. This will help unload Vuforia which otherwise would fail to be deinitialized. We recommend always calling UnloadVuforia() before Application.Unload() to avoid unexpected behaviors.

Add the function call in the ‘Unload’ button UI.

Void OnGUI()
{
  If(GUI.Button(new Rect(10, 400, 400, 100), “Unload”, style))
  {
    Vuforia.VuforiaRuntime.Instance.UnloadVuforia();
    Application.Unload();
  }
}

 

Android Specific Steps

After following the steps in the general Unity guide, apply the following steps before clicking “Sync Now”:

  • Vuforia Engine (as well as the Unity 2019.3 beta versions) require the minSdkVersion to be set to 19 in the android – defaultConfig section of the build.gradle (Module: app).

  • Vuforia Engine bundles libraries for all Android architectures supported by Unity: ARMv7, ARM64 and x86. Assuming that you exported your Unity project with ARMv7 support only (which is the default), you’ll need to filter for that architecture. Otherwise, the app will fail to load the Unity libmain.so on ARM64 devices since that folder only contains the .so files for Vuforia, but not Unity.
    To do that, add this section to the same section as above:

ndk { abiFilters 'armeabi-v7a' }

  • Click “Sync now”:

  • NOTE: the gradle structure path for the UnityLibrary should be under allprojects/repositories/
allprojects
{
  repositories
  {
    flatDir
    {
      dirs "$ {project(':unityLibrary').projectDir} /libs"
    }
  }
}
  • Now sync the gradle project a last time, it should build and run fine now: 

iOS specific steps

After following the steps in the general Unity guide, apply the following steps in Xcode:

  • Add Vuforia.framework
    • In the NativeiOSApp project, under General / Embedded Binaries, press +
    • Click on Add Other…
    • Select the iosBuild/Frameworks/com.ptc.vuforia.engine/Vuforia/Plugins/iOS/Vuforia.framework and Open.
    • Delete (press -) on the linked Frameworks and Libraries on Vuforia.framework.

Add Camera usage description

  • Even if the camera usage description was set in Unity, it needs to be added to the NativeiOSApp project:
  • From the tabs, select Info
  • At Custom iOS Target Properties, Hover over any of the currently applied properties and press +
  • From the list choose Privacy - Camera Usage Description
  • Set it to something like “Required for augmented reality support.”

Move the Vuforia folder

  • In order to be able to load Vuforia target databases at runtime, the “Vuforia” folder from the Unity-iPhone project needs to be moved to the NativeiOSApp project.
  • After it has been moved or copied, set its Target Membership to “NativeiOSApp”: