Unity MR Part 9: Instantiate Prefab
tststs
Posted on January 11, 2024
š Stumbled here on accident? Start with the introduction!
š The objective of this article is to demonstrate how to instantiate a Prefab ā in our example, a door ā and subsequently anchor it within the MR environment. This process is key to effectively integrating virtual objects, like the door, into the MR space, ensuring they are correctly positioned and maintained within the real-world context.
ā¹ļø If you find yourself facing any difficulties, remember that you can always refer to or download the code from our accompanying GitHub repository
First, let's create the door Prefab required for this article. Download the following archive door.rar and extract its contents into your Assets/3DModel folder. Confirm the following dialog with Fix now
after dropping the files.
Then, create a new Prefab in your Assets/Prefabs folder using the same method as we did for the reticle in a previous article. Once you've completed these steps, your Prefab should look like this:
We are now set to instantiate the door Prefab and anchor it within the environment. This action will occur when the user hovers over a valid plane and presses the Trigger
button.
Here's how the updated MRArticleSeriesController
script will look:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.Interaction.Toolkit;
namespace Taikonauten.Unity.ArticleSeries
{
public class MRArticleSeriesController : MonoBehaviour
{
public ActionBasedController controller;
public InputActionReference buttonAction;
public XRRayInteractor rayInteractor;
public ARAnchorManager anchorManager;
public GameObject door;
void OnEnable()
{
Debug.Log("MRArticleSeriesController -> OnEnable()");
buttonAction.action.performed += OnButtonPressedAsync;
}
void OnDisable()
{
Debug.Log("MRArticleSeriesController -> OnDisable()");
buttonAction.action.performed -= OnButtonPressedAsync;
}
private async void OnButtonPressedAsync(InputAction.CallbackContext context)
{
Debug.Log("MRArticleSeriesController -> OnButtonPressed()");
if (rayInteractor.TryGetCurrent3DRaycastHit(out RaycastHit hit))
{
Pose pose = new(hit.point, Quaternion.identity);
Result<ARAnchor> result = await anchorManager.TryAddAnchorAsync(pose);
result.TryGetResult(out ARAnchor anchor);
if (anchor != null)
{
// Instantiate the door Prefab
GameObject \_door = Instantiate(door, hit.point, Quaternion.identity);
// Unity recommends parenting your content to the anchor
// instead of adding a ARAnchor component to the GameObject
\_door.transform.parent = anchor.transform;
}
}
}
}
}
Let's review the updates quickly:
- A public GameObject member named
door
has been added, enabling us to select the door Prefab in the Inspector. - Instead of simply logging the anchor instance, we now verify if the returned anchor is not null.
- The
door
Prefab is instantiated at the raycast hit point. - The
door
(our content) is parented to the anchor, following Unity's recommended practices.
With these updates to the MRArticleSeriesController
Script, don't forget to select the door
Prefab in the Inspector to complete the setup.
To test the application, choose Build and Run
, as detailed in the previous articles. Hover over a Plane and press the Trigger
button. If all configurations are correct, you should see a similar result as seen in the next video:
Next article
In our forthcoming article, we will take an exciting step in our MR journey by integrating the VoiceSDK
. This addition will enable voice interaction within our MR environment, particularly focusing on using voice commands to interact with the door we've created. We'll guide you through the process of integrating the VoiceSDK
into your Unity project, setting up voice recognition, and configuring it to recognize specific commands -- such as "open the door".
Posted on January 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.