"We offer new support options and therefor the forums are now in read-only mode! Please check out our Support Center for more information." - Vuforia Engine Team

Load dataset from Android split binary (obb)

A previous thread has described a few different approaches to handling an Android app that exceeds 100mb in size: https://developer.vuforia.com/forum/faq/unity-how-can-i-handle-large-android-apps

This thread will provide a small example of how to load a dataset from the obb file when using the split binary option.

Create a new Unity project and import the Vuforia SDK and Vuforia Sample Project. Download here: https://developer.vuforia.com/downloads/sdk

Vuforia Configuration

  •     Enter your license key
  •     Make sure your dataset is loaded and activated (in this example I will be using the StonesAndChips dataset present in the sample project)

Player Settings -> Android Settings

  •     Other Settings: Set "Write Permissions" to "External (SDCard)"
  •     Publishing Settings: Check the "Split Application Binary" box

Once the above is all set up, copy the code at the bottom of this post (ObbExtractor.cs) and create a new scene. In this scene, create a new script with the code below and attach it to an object. This scene will need to be run before Vuforia is initialized to function properly.

For this example, add the Vuforia sample project scenes to the build settings' "Scenes in Build" and then add this extra scene with the ObbExtractor at the top of the list of scenes. This will start the app in this scene and allow the datasets to be extracted before moving on to the Vuforia initialization in the following scenes. The sample script provided in this case will automatically load the scene "Vuforia-0-Splash" when the extraction has been completed.

Once Vuforia has been loaded, the image target for the StonesAndChips dataset will be ready to go.

using System.IO;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;


public class ObbExtractor : MonoBehaviour {

    void Start () {
        StartCoroutine(ExtractObbDatasets());
    }

    private IEnumerator ExtractObbDatasets () {
        string[] filesInOBB = {"VuforiaMars_Images.dat", "VuforiaMars_Images.xml"};
        foreach (var filename in filesInOBB) {
            string uri = Application.streamingAssetsPath + "/Vuforia/" + filename;

            string outputFilePath = Application.persistentDataPath + "/Vuforia/" + filename;
            if(!Directory.Exists(Path.GetDirectoryName(outputFilePath)))
                Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath));

            var www = new WWW(uri);
            yield return www;

            Save(www, outputFilePath);
            yield return new WaitForEndOfFrame();
        }

        // When done extracting the datasets, Start Vuforia AR scene
        SceneManager.LoadScene( "0-Splash" );
    }

    private void Save(WWW w, string outputPath) {
        File.WriteAllBytes( outputPath, w.bytes );

        // Verify that the File has been actually stored
        if( File.Exists( outputPath ) )
        {
            Debug.Log( "File successfully saved at: " + outputPath );
        }
        else
        {
            Debug.Log( "Failure!! - File does not exist at: " + outputPath );  
        }
    }
}

This code will pull the StonesAndChips .dat and .xml files from the obb and output them in the applications persistentDataPath, allowing them to be loaded once Vuforia is initialized. This code will need to run before Vuforia is initialized.

Edit (March 7, 2018): Updated dataset reference to Mars datasets used in current versions of SDK. Updated StreamingAssets path to point to the "Vuforia" sub-directory as "QCAR" is no longer used post Vuforia 6.5. Updated the name of the scene to load into as this has changed in our samples.