Working with Vuforia Engine and Unity¶
This article holds common how-to guides for authoring digital content for your Vuforia Engine targets. Learn how to add content at runtime, swap 3D models on a target or implement on-screen interaction logic for intuitive control for your users.
- How to Dynamically Add Content to Targets in Unity
- How to Dynamically Swap a 3D Model of a Target in Unity
- How to Drag an Augmentation using Touch Input
How to Dynamically Add Content to Targets in Unity¶
The following example demonstrates how you can augment an Image Target with a custom 3D model that is instantiated at run time, upon target detection, using Vuforia Engine. The script does the following: it dynamically adds a 3D model to the Astronaut Image Target.
Follow these steps:
- Open or create a Unity project, add an ImageTarget GameObject. Either import the default sample database with the Astronaut image or import and select your custom database with an Image Target.
- In the Project View, under the
Assets
folder, create a subfolder calledPrefabs
. -
Add a prefab object to the
Prefabs
folder. There are many ways to create custom prefabs to represent 3D objects. For instance, you could:-
Create a simple Cube GameObject in your scene view and then drag it from the scene view into the
Prefabs
folder in the Project view,OR
-
Import a 3D model in a format supported by Unity (such as
FBX
,OBJ
,DAE
, or3DS
). Refer to the Unity website for details on how to create Prefabs from 3D models from various file formats.
-
-
Create a C# script, call it
MyPrefabInstantiator
, and attach it to the Astronaut Image Target. -
Copy the following code into the script and then save the script:
The
MyPrefabInstantiator
class inherits from the DefaultObserverEventHandler class, that takes care of handling events when the target is detected. At that point, the Prefab 3D model gets instantiated and gets attached on the Image Target. -
Return to the Unity scene view and click on the Astronaut Image Target, in the inspector you will see that the
MyPrefabInstantiator
script exposing a field called My Model Prefab.- Select your prefab in the
Prefabs
folder (in the project view) and drag it onto the My Model Prefab field in theMyPrefabInstantiator
script component..
- Select your prefab in the
-
Save the project, and then build and run the app or use the Play Mode. As soon as the Image Target is detected and TRACKED, your prefab 3D model should appear on top of it.
- If the model size is either too small or too big, you can go to the script code and adjust the
localScale
values.
How to Dynamically Swap a 3D Model of a Target in Unity¶
This section explains how you can swap the 3D model of an Image Target (or other target) with a different 3D model at run time.
- Open or create a Unity project, add an ImageTarget GameObject.
- Add a 3D model as a child of the Image Target, for example a sphere.
- Create a C# script, name it
ModelSwapper
. This script will display a button on top of the camera view that when pressed, lets you swap one model with another 3D model. In our example, this will be a cube. -
Insert the following code into the
ModelSwapper
script: -
Attach the script to the ARCamera GameObject in your scene view,
- In the inspector view, drag your target GameObject to the field mTarget.
- Test the script with Play Mode and press the Swap model button to disable the sphere and create a cube as a child of the target.
How to Drag an Augmentation using Touch Input¶
This section provides the sample code of a script that lets users drag an AR object on the screen with their finger,
- Open or create a new Unity Project. Add the ARCamera and a Vuforia target GameObject.
- Add a 3D sphere or other 3D content as a child to the target GameObject. The content must have a mesh collider.
- Create a new script named
DragObject.cs
. Add the following code to it and attach the script to the ARCamera. The script enables users to touch and drag an object around