Instant Image Targets

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

Create Image Targets in the Target Manager and download them as a Device Database or create them instantly from an image asset at runtime.

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

Image Target from an Image Asset

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

This method provides less information on the tracking quality and rating of the image asset than the Target Manager. You should, therefore, choose an image source that fulfills the best practices. CreateImageTarget() has a limit of 2048 pixel size for the width and height.

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.
    • You may 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 at 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 deploying 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 an Image Target at runtime. 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 the 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. 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);
    }
}

 

Can this page be better?
Share your feedback via our issue tracker