Instant Image Targets

Create Image Targets instantly from an image asset stored locally or externally.

Image Targets can be created in the Vuforia Target Manager and then downloaded as a device database, or they can be created directly and instantly from an image asset.

If you are looking for runtime loading Image Targets in Native, please refer to How to Create and Modify Targets in Native.

Create an Image Target from an Image Asset

Use an image asset with sufficient details and load it in the Unity Editor to create an Image Target from it. Add the image asset to the ImageTargetBehaviour component of the Image Target GameObject and Vuforia will create the Image Target at runtime.

Using this method will not, as the Vuforia Target Manager does, provide information of the tracking quality and rating of the image asset. You should therefore choose an image source that fulfills the best practices.

Create instant Image Target

  1. Open an empty Unity Project and add the Vuforia SDK.
  2. Create an ARCamera GameObject and add a license from your Vuforia Developer account into the license key field in VuforiaConfiguration.
  3. Load a detailed JPG or PNG image into the Assets folder.
  4. Create a new Vuforia Engine ImageTarget GameObject.

  5. In the Inspector of the ImageTarget GameObject, set the Type to From Image.
  6. Drag the image file to the empty box labeled Image and give it a name.
    • It may be that you need to set the image to “Read/Write Enabled” in the advanced import settings. The inspector will display a message if additional actions are required or if the image is in risk of being rescaled upon target creation.

      NOTE: The expandable Advanced menu is for rescaling the ImageTarget GameObject.

  7. Place your AR content as a child on the ImageTarget GameObject and use Vuforia's Play Mode by pressing Play or deploy the app to your device.

Create Instant Image Targets at Runtime from a file or Texture2D Objects

Alternatively, Instant Image Targets can be created at runtime in C#. Attach the following code to any GameObject in the scene. The script loads an image file from the StreamingAssets/Vuforia folder and creates at runtime an Image Target from it. For more information on runtime loading, please see Create and Load Targets in Unity.

NOTE: A license key must be added to use instant Image Targets.

using UnityEngine;
using Vuforia;
public class SideLoadImageTarget : MonoBehaviour
{
    public Texture2D textureFile;
    public float printedTargetSize;
    public string targetName;
    void Start()
    {
       // Use Vuforia Application to invoke the function after Vuforia Engine is initialized
       VuforiaApplication.Instance.OnVuforiaStarted += CreateImageTargetFromSideloadedTexture;
    }
    void CreateImageTargetFromSideloadedTexture()
    {
        var mTarget = VuforiaBehaviour.Instance.ObserverFactory.CreateImageTarget(
            textureFile,
            printedTargetSize,
            targetName);
        // add the Default Observer Event Handler to the newly created game object
        mTarget.gameObject.AddComponent<DefaultObserverEventHandler>();
        Debug.Log("Instant Image Target created " + mTarget.TargetName);
    }
}

Image Target from an external texture2D source

The code example below downloads a texture image from a web URL and generates an Image Target from it. Attach the script to the ARCamera GameObject.

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using Vuforia;
public class SideLoadFromWeb : MonoBehaviour
{
    Texture2D imageFromWeb;
    void Start()
    {
        VuforiaApplication.Instance.OnVuforiaInitialized += OnVuforiaInitialized;
    }
    void OnVuforiaInitialized(VuforiaInitError error)
    {
        StartCoroutine(RetrieveTextureFromWeb());
    }
    IEnumerator RetrieveTextureFromWeb()
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture("https://library.vuforia.com/sites/default/files/vuforia-library/articles/solution/Magic%20Leap%20Related%20Content/Astronaut-scaled.jpg"))
        {
            yield return uwr.SendWebRequest();
            if (uwr.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                // Get downloaded texture once the web request completes
                var texture = DownloadHandlerTexture.GetContent(uwr);
                imageFromWeb = texture;
                Debug.Log("Image downloaded " + uwr);
                CreateImageTargetFromDownloadedTexture();
            }
        }
    }
    void CreateImageTargetFromDownloadedTexture()
    {
        var mTarget = VuforiaBehaviour.Instance.ObserverFactory.CreateImageTarget(
    imageFromWeb,
    0.1f,
    "Astronaut");
        // Add the DefaultObserverEventHandler to the newly created game object
        mTarget.gameObject.AddComponent<DefaultObserverEventHandler>();
        Debug.Log("Target created and active" + mTarget);
    }
}