Log in or register to post comments

Create ImageTarget in code

April 11, 2018 - 7:19am #1

Hello, I want to create ImageTarget in my code. When I create Empty object and add ImageTargetBehavior, then I don't know, how to change database and image. Please, can you help me?

Create ImageTarget in code

April 11, 2018 - 9:26am #3

Hello balaz94,

The following script is a short example of how to step through all your trackables and assign components or augmentations to them. You can reference the trackable's name in this loop to determine which object it is and thus how to modify it.

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

public class CreateImageTarget : MonoBehaviour {

    public GameObject augmentationObject;

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 Support

Create ImageTarget in code

April 11, 2018 - 8:35am #2
Log in or register to post comments