Load Vuforia Targets from the Android App-specific storage in Unity

Vuforia Engine lets you load Vuforia targets from app-specific storage on Android. Dataset (.xml and .dat) files that are downloaded at runtime can be saved to that location and loaded using the following example.

Load from App-specific Storage

Unity provides an abstraction for app-specific storage, which is accessible using the persistentDataPath.

externalPath = UnityEngine.Application.persistentDataPath;

On most devices, the persistent datapath looks like this:

/storage/emulated/0/data/ + <package-name> + /files

For example, if your package name is com.myorg.myapps, you should see:

/storage/emulated/0/Android/data/com.myorg.myapps/files

Based on the above information, you should store your dataset files in the above directory once they are downloaded. For example, for the VuforiaMars_Images database, you should copy the dataset files into a subdirectory in /files/ or into the following directory:

/storage/emulated/0/Android/data/com.myorg.myapps/files/VuforiaMars_Images.xml
/storage/emulated/0/Android/data/com.myorg.myapps/files/VuforiaMars_Images.dat

The following code snippet shows how to load dataset files from the app-specific storage in a C# script. In the following example, the database is called VuforiaMars_Images (dataset files VuforiaMars_Images.xml and VuforiaMars_Images.dat) and the target is called Astronaut.  The database was saved directly into UnityEngine.Application.persistentDataPath:

using UnityEngine;
using Vuforia;

public class AppStorageDataSetLoader : MonoBehaviour
{
    private void Start()
    {
        VuforiaApplication.Instance.OnVuforiaStarted += LoadObserver;
    }

    void LoadObserver()
    {
        string externalPath = Application.persistentDataPath + "/VuforiaMars_Images.xml";
        string target = "Astronaut";

        // Create target from path and target name
        var imageTarget = VuforiaBehaviour.Instance.ObserverFactory.CreateImageTarget(externalPath, target);
    }
}