Topic locked

Load dataset from Android split binary (obb)

August 2, 2017 - 8:29am #1

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.

Disclaimer: This code is meant as an instructional example for Vuforia 6.2 sample app.

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


public class ObbExtractor : MonoBehaviour {

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

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

            string outputFilePath = Application.persistentDataPath + "/QCAR/" + 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( "Vuforia-0-Splash" );
    }

    private void Save(WWW www, string outputPath) {
        File.WriteAllBytes( outputPath, <a href="http://www.bytes">www.bytes</a> );

        // 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.

Topic locked