Search Unity

Disable automatic instantiation of ImageTargets for each Target in Library

Discussion in 'Vuforia' started by vinnie-vivace, Oct 3, 2017.

  1. vinnie-vivace

    vinnie-vivace

    Joined:
    Jan 12, 2010
    Posts:
    40
    Is there a setting that disables this behaviour? I have a library with multiple targets in it, however I don't necessarily want ImageTargets instantiated for each target in every scene.

    for example, my current library has 2 targets, and I have an ImageTargetBehaviour associated with one of them, when i run the scene I get an additional "new game object" that's associated with the other one. It serves no purpose other than using resources unnecessarily.

    thanks.
     
  2. Augmenteer

    Augmenteer

    Joined:
    Aug 30, 2015
    Posts:
    43
    Those are ImageTargetBehaviours that are instantiated because they are active in the target database - all targets in a DB are activated when the DB is activated.

    If you want to really optimize runtime resource management, you can manage your database(s) programmatically.
    In your case, you might want to create multiple databases which contain a specific set of targets to be activated simultaneously - or you could even create a unique DB for each target. And then (de)activate as needed.

    FWIW the ITB GOs shouldn't be costing you much either computationally or memory-wise. They're pretty efficient.
     
    Vuforia-Strasza likes this.
  3. vinnie-vivace

    vinnie-vivace

    Joined:
    Jan 12, 2010
    Posts:
    40
    thanks for the response. Rather than have multiple databases to maintain, I just created a script that looks for the unwanted ImageTargetBehaviours at runtime and deletes them. Good to know they are efficient however I see no need to leave unwanted behaviours hanging around.

    cheers.
     
  4. Spark88

    Spark88

    Joined:
    Dec 12, 2015
    Posts:
    4
    That's correct,
    one way to do that is to have a script like the below attached to each Image Tracker object

    using UnityEngine;
    using Vuforia;
    //Attach to the image tracker
    public class ChildObjectsActivator : MonoBehaviour, ITrackableEventHandler
    {
    private TrackableBehaviour trackableBehaviour;
    void Start()
    {
    trackableBehaviour = GetComponent<TrackableBehaviour>();
    if (trackableBehaviour)
    trackableBehaviour.RegisterTrackableEventHandler(this);
    }

    public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
    {
    if (newStatus == TrackableBehaviour.Status.DETECTED ||
    newStatus == TrackableBehaviour.Status.TRACKED ||
    newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    OnTrackingFound();
    else
    onTrackingLost();
    }
    private void OnTrackingFound()
    {
    if (transform.childCount > 0)
    SetChildrenActive(true);
    }
    private void onTrackingLost()
    {
    if (transform.childCount > 0)
    SetChildrenActive(false);
    }
    private void SetChildrenActive(bool activeState)
    {
    for (int i = 0; i <= transform.childCount; i++)
    transform.GetChild(i++).gameObject.SetActive(activeState);
    }