Search Unity

How to switch between databases at runtime?

Discussion in 'Vuforia' started by mnarimani, Aug 21, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    211
    Hi,
    I have 10 databases. I want to change the active database in "Image Target Behaviour" at runtime. How can I do that?
     
  2. glutvort

    glutvort

    Joined:
    Apr 13, 2019
    Posts:
    31
    delete current data, init next data
    code from here https://developer.vuforia.com/forum/faq/unity-load-dataset-setup-trackables-runtime
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. using Vuforia;
    6. using System.Collections.Generic;
    7.  
    8.  
    9. public class DynamicDataSetLoader : MonoBehaviour
    10. {
    11.     // specify these in Unity Inspector
    12.     public GameObject augmentationObject = null;  // you can use teapot or other object
    13.     public string dataSetName = "";  //  Assets/StreamingAssets/QCAR/DataSetName
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         // Vuforia 5.0 to 6.1
    19.         VuforiaBehaviour vb = GameObject.FindObjectOfType<VuforiaBehaviour>();
    20.         vb.RegisterVuforiaStartedCallback(LoadDataSet);
    21.  
    22.         // Vuforia 6.2+
    23.         VuforiaARController.Instance.RegisterVuforiaStartedCallback(LoadDataSet);
    24.     }
    25.        
    26.     void LoadDataSet()
    27.     {
    28.  
    29.         ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
    30.        
    31.         DataSet dataSet = objectTracker.CreateDataSet();
    32.        
    33.         if (dataSet.Load(dataSetName)) {
    34.            
    35.             objectTracker.Stop();  // stop tracker so that we can add new dataset
    36.  
    37.             if (!objectTracker.ActivateDataSet(dataSet)) {
    38.                 // Note: ImageTracker cannot have more than 100 total targets activated
    39.                 Debug.Log("<color=yellow>Failed to Activate DataSet: " + dataSetName + "</color>");
    40.             }
    41.  
    42.             if (!objectTracker.Start()) {
    43.                 Debug.Log("<color=yellow>Tracker Failed to Start.</color>");
    44.             }
    45.  
    46.             int counter = 0;
    47.  
    48.             IEnumerable<TrackableBehaviour> tbs = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
    49.             foreach (TrackableBehaviour tb in tbs) {
    50.                 if (tb.name == "New Game Object") {
    51.  
    52.                     // change generic name to include trackable name
    53.                     tb.gameObject.name = ++counter + ":DynamicImageTarget-" + tb.TrackableName;
    54.  
    55.                     // add additional script components for trackable
    56.                     tb.gameObject.AddComponent<DefaultTrackableEventHandler>();
    57.                     tb.gameObject.AddComponent<TurnOffBehaviour>();
    58.  
    59.                     if (augmentationObject != null) {
    60.                         // instantiate augmentation object and parent to trackable
    61.                         GameObject augmentation = (GameObject)GameObject.Instantiate(augmentationObject);
    62.                         augmentation.transform.parent = tb.gameObject.transform;
    63.                         augmentation.transform.localPosition = new Vector3(0f, 0f, 0f);
    64.                         augmentation.transform.localRotation = Quaternion.identity;
    65.                         augmentation.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f);
    66.                         augmentation.gameObject.SetActive(true);
    67.                     } else {
    68.                         Debug.Log("<color=yellow>Warning: No augmentation object specified for: " + tb.TrackableName + "</color>");
    69.                     }
    70.                 }
    71.             }
    72.         } else {
    73.             Debug.LogError("<color=yellow>Failed to load dataset: '" + dataSetName + "'</color>");
    74.         }
    75.     }
    76. }
     
    meedabit likes this.
  3. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    211
    This code is really confusing and doesn't help.
    It doesn't explain what each line does. Because of that I cannot apply this to my use case.