Log in or register to post comments

How to recognize targets from the database, without adding many separated ImageTraget manually?

July 10, 2018 - 2:09pm #1

Hi all! I'm new to making Unity+Vuforia apps and now I've made only an application in which all the targets are located in one scene as separate ImageTarget objects and on top of them are the objects that are displayed when target is recognised. Simply put, I did as shown in this (and other similar) videos: https://youtu.be/kXwcjBZxQKo?t=7m43s

Now I want to make an app with large number of targets and creating such a large number of objects for this looks weird. Is it possible to somehow substitute all the targets into one object without creating a separate ImageTraget for each target?

Many thanks!

How to recognize targets from the database, without adding many separated ImageTraget manually?

July 12, 2018 - 3:10pm #3

Thank you so much! :) This is exactly what I needed! 

How to recognize targets from the database, without adding many separated ImageTraget manually?

July 11, 2018 - 3:27pm #2

Hello Dudnikov,

You can programmatically set these objects up. The following function demonstrates a way to loop through all trackable targets in active datasets and attach some simple components and augmentations on them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class CreateImageTarget : MonoBehaviour {

    public GameObject augmentationObject;

    // Start once Vuforia is initialized
    void Start () {
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
    }

    void OnVuforiaStarted()
    {
        int counter = 0;

        // Loop through all activated trackables
        IEnumerable<TrackableBehaviour> tbs = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
        foreach (TrackableBehaviour tb in tbs) {
            // This will filter by trackables that haven't been defined previously in the scene.
            if (tb.name == "New Game Object") {

                // Change generic name to include trackable name
                tb.gameObject.name = ++counter + ":DynamicImageTarget-" + tb.TrackableName;

                // Add additional script components for trackable
                tb.gameObject.AddComponent<DefaultTrackableEventHandler>();
                tb.gameObject.AddComponent<TurnOffBehaviour>();

                // This section will add an augmentation based off the GameObject defined on the script.
                // Replace this with whatever you prefer to augment
                if (augmentationObject != null) {
                    // instantiate augmentation object and parent to trackable
                    GameObject augmentation = (GameObject)GameObject.Instantiate(augmentationObject);
                    augmentation.transform.parent = tb.gameObject.transform;
                    augmentation.transform.localPosition = new Vector3(0f, 0f, 0f);
                    augmentation.transform.localRotation = Quaternion.identity;
                    augmentation.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
                    augmentation.gameObject.SetActive(true);
                } else {
                    Debug.Log("<color=yellow>Warning: No augmentation object specified for: " + tb.TrackableName + "</color>");
                }
            }
        }
    }
}

Thanks,

Vuforia Engine Support

Log in or register to post comments