Skip to content

State-Based Model Targets in Unity

Import your State-Based Model Targets into Unity and author your AR experience.

Refer to State-Based Model Targets API Overview for a guide using the native C++ API.

Unity Setup

Import State-Based Model Targets into Unity as you would with standard Model Targets. Select the database from a ModelTargetBehaviour to track and report its status and status info like any other Model Target.

Reporting and visualizing active states

You can report the currently recognized or manually set state with the ModelTarget.GetActiveStateName() method. Remember that it is not guaranteed that the currently recognized state is the exact same state the target actually is in.

It is also possible to visualize the currently active state by rendering its geometry with an outline or any other shader. This is best done by selecting Add Target Representation from the inspector of the ModelTargetBehaviour. This will add a representation for every state, named after the state's name.

The following script demonstrates getting the representations from a Model Target GameObject, applying a custom material to every mesh of the representation, and logging the currently active state in the console.

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

public class StateBasedVisualization : MonoBehaviour
{
    public ModelTargetBehaviour ModelTarget;
    public GameObject RepresentationRootNode;
    public Material RenderMaterial;

    private string mCurrentStateName = string.Empty;
    private GameObject mCurrentStateRepresentation = null;
    private Dictionary<string, GameObject> mStateRepresentations = new Dictionary<string, GameObject>();

    private void FindRepresentations()
    {
        foreach (Transform t in RepresentationRootNode.transform)
        {
            mStateRepresentations[t.gameObject.name] = t.gameObject;
            t.gameObject.SetActive(false);
        }
    }

    private void ApplyMaterials()
    {
        foreach (MeshRenderer mr in RepresentationRootNode.GetComponentsInChildren<MeshRenderer>())
        {
            mr.material = RenderMaterial;
        }
    }

    private void UpdateState()
    {
        string currentStateName = ModelTarget.GetActiveStateName();
        if (!currentStateName.Equals(mCurrentStateName))
        {
            Debug.Log("New State detected: " + currentStateName);
            if (mCurrentStateRepresentation != null)
            {
                mCurrentStateRepresentation.SetActive(false);
            }

            mCurrentStateName = currentStateName;
            mCurrentStateRepresentation = mStateRepresentations[mCurrentStateName];
            mCurrentStateRepresentation.SetActive(true);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        ApplyMaterials();
        FindRepresentations();
        UpdateState();
    }

    // Update is called once per frame
    void Update()
    {
        UpdateState();
    }
}

Add your Model Target GameObject, its Representation Root Node, and a custom Render Material to the script's public fields.

The Render Material used in our example uses the Transparent/VertexLit with Z shader and has a main color with a lower alpha value.

Manually set the active state

SetActiveStateName() is for manually switching the state of a Model Target, and it is necessary to call this function when you use standard (non-trained) State-Based Model Targets with Guide Views to switch them to a specific state. This could, for example, be done from a UI that allows the user to go through the different actions of an assembly procedure.

The GetAvailableStateNames() returns all available state names of the Model Target.

Learn More

State-Based Model Targets

Illustration Files and Process Plans for State-Based Model Targets

Generate State-Based Model Targets

State-Based Model Targets in Native