QCAR 加载 激活 删除dataset

来源:互联网 发布:剑三成男捏脸数据大 编辑:程序博客网 时间:2024/06/01 09:13

Swappable Device Databases

The DataSetLoadBehaviour is a script that is by default attached to the ARCamera object. The script exposes a list of device databases (that are part of the project) to be loaded at scene startup. It also contains a dropdown list to choose a device database to be activated. You can use different settings inDataSetLoadBehaviour across different scenes. This enables you to bind device databases implicitly to Unity scenes. Be aware that this script was not created to provide a runtime API. It loads/activates the device databases only at startup, but doesn’t do anything when its properties are changed later in the application lifecycle. The Runtime Dataset API needs to be used to change device databases at runtime.

Runtime device database API

Mainly three classes are used to load/unload and activate/deactivate databases at runtime –TrackerManagerImageTracker and DataSet.

Loading a dataset

  1. Check whether the file at the given storage location exists: DataSet.Exists (string path, DataSet.StorageType storageType);

Note: You can also call "Exists" without specifying a storage type. The assumption, then, is that your devce database has been bundled with the application in "StreamingAssets/QCAR."

Note: You can also call “Exists” without specifying a storage type. The assumption then is that your device database has been bundled with the application in “StreamingAssets/QCAR.”

  1. Request an ImageTracker instance from TrackerManager:TrackerManager.Instance.GetTracker (Tracker.Type.IMAGE_TRACKER);
  2. Create a new DataSet instance: ImageTracker.CreateDataSet();
  3. Load a dataset file from its storage location: DataSet.Load(string path, DataSet.StorageType storageType);

The same storage type rules apply as for “Exists”.

Activating a dataset

  1. Use the imageTracker instance to activate a dataset ImageTracker.ActivateDataSet(DataSet dataset);

Example code:

// Load and activate a data set at the given path.private bool LoadDataSet(string dataSetPath, DataSet.StorageType storageType){    // Check if the data set exists at the given path.    if (!DataSet.Exists(dataSetPath, storageType))    {        Debug.LogError("Data set " + dataSetPath + " does not exist.");        return false;    }      // Request an ImageTracker instance from the TrackerManager.    ImageTracker imageTracker =        (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER);      // Create a new empty data set.    DataSet dataSet = imageTracker.CreateDataSet();      // Load the data set from the given path.    if (!dataSet.Load(dataSetPath, storageType))    {        Debug.LogError("Failed to load data set " + dataSetPath + ".");        return false;    }      // (Optional) Activate the data set.    imageTracker.ActivateDataSet(dataSet);             return true;} 

Adding content to a target

In Vuforia 2.0, DataSets now contain Trackables, whereas TrackableBehaviours represent aTrackable result at runtime. All TrackableBehaviours for the currently loaded Trackables can be queried using StateManager.GetTrackableBehaviours().
To add content to the TrackableBehaviour for a specific dataset at runtime, follow these steps:

  1. Request the TrackableBehaviours from the StateManager: TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
  2. When iterating over them, check if the TrackableBehaviour belongs to the DataSet:
    if (dataSet.Contains(trackableBehaviour.Trackable))
  3. Attach a TrackableEventHandler to the game object of the TrackableBehaviour: GameObject.AddComponent<MyTrackableEventHandler>(); write your own implementation of the ITrackableEventHandler interface, or use the DefaultTrackableEventHandler that comes with the QCAR extension. The TrackableEventHandler provides callbacks to handle Target lost and found events.
  4. Add a child object for augmentation to the TrackableBehaviour. GameObject.transform.parent = DataSetTrackableBehaviour.transform;

Example code:


// Add Trackable event handler and content (cubes) to the Targets. private void AttachContentToTrackables(DataSet dataSet) {     // get all current TrackableBehaviours     IEnumerable<TrackableBehaviour> trackableBehaviours = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();      // Loop over all TrackableBehaviours.     foreach (TrackableBehaviour trackableBehaviour in trackableBehaviours)     {         // check if the Trackable of the current Behaviour is part of this dataset         if (dataSet.Contains(trackableBehaviour.Trackable))         {             GameObject go = trackableBehaviour.gameObject;              // Add a Trackable event handler to the Trackable.             // This Behaviour handles Trackable lost/found callbacks.             go.AddComponent<DefaultTrackableEventHandler>();              // Create a cube object.             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);              // Attach the cube to the Trackable and make sure it has a proper size.             cube.transform.parent = trackableBehaviour.transform;             cube.transform.localScale = new Vector3(0.7f, 0.7f, 0.7f);             cube.transform.localPosition = new Vector3(0.0f, 0.35f, 0.0f);             cube.transform.localRotation = Quaternion.identity;             cube.active = true;             trackableBehaviour.gameObject.active = true;         }     } }

Destroying a dataset

  1. Request an ImageTracker instance as explained above.
  2. Make sure that the dataset to destroy is not active (otherwise destruction will fail):ImageTracker.Deactivate(DataSet dataset);
  3. Destroy the dataset: ImageTracker.DestroyDataSet(DataSet dataset, bool destroyTrackableGameObjects);

Example code:

// Destroy a dataset.private bool DestroyDataSet(DataSet dataSet){    // Request an ImageTracker instance from the TrackerManager.    ImageTracker imageTracker = (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER);    // Make sure the data set is not active.    imageTracker.DeactivateDataSet(dataSet);      // Destroy the data set    // (choose true to also destroy the Target objects that belong to the data set).    bool success = imageTracker.DestroyDataSet(dataSet, true);     return success;}