GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube) GameObject mExistingModel = mTarget.gameObject įor (int i = 0 i < i++) Public class ModelSwapper : MonoBehaviour Insert the following code into the ModelSwapper script:.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.Add a 3D model as a child of the Image Target, for example a sphere.Open or create a Unity project, add an ImageTarget GameObject.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. How to Dynamically Swap a 3D Model of a Target in Unity If the model size is either too small or too big, you can go to the script code and adjust the localScale values.As soon as the Image Target is detected and TRACKED, your prefab 3D model should appear on top of it. Save the project, and then build and run the app or use the Play Mode.Select your prefab in the Prefabs folder (in the project view) and drag it onto the My Model Prefab field in the MyPrefabInstantiator script component.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.At that point, the Prefab 3D model gets instantiated and gets attached on the Image Target. The MyPrefabInstantiator class inherits from the DefaultObserverEventHandler class, that takes care of handling events when the target is detected. MMyModelObject = Instantiate(myModelPrefab, ansform) Instantiate the model prefab only if it hasn't been instantiated yetĭebug.Log("Target found, adding content") Protected override void OnTrackingFound() Public class MyPrefabInstantiator : DefaultObserverEventHandler Copy the following code into the script and then save the script:.Create a C# script, call it MyPrefabInstantiator, and attach it to the Astronaut Image Target.Refer to the Unity website for details on how to create Prefabs from 3D models from various file formats. Import a 3D model in a format supported by Unity (such as FBX, OBJ, DAE, or 3DS).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,.There are many ways to create custom prefabs to represent 3D objects. Add a prefab object to the Prefabs folder.In the Project View, under the Assets folder, create a subfolder called Prefabs.Either import the default sample database with the Astronaut image or import and select your custom database with an Image Target. 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. How to Dynamically Add Content to Targets in Unity
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |