This workshop will show you how to:

To complete this workshop you will need:
Language used:
Additional resources:
This workshop will show you how to setup a virtual reality environment using the Unity XR interaction toolkit; how to interact with the virtual objects in the scene; how to move around the environment using a teleportation system; how to develop a custom script to interact with a MQTT Broker.
Create a new Universal Render Pipeline (URP) Unity project using UnityHub.

As the Oculus Quest 2 is based on Android, we need to change the target platform from the build settings. From File -> Build Settings select Android and press Switch Platform.
Install the XR Integration Toolkit from the Package Manager window, and import the Default Input Actions from the Sample menu. If prompted, enable the new input system.

Once the plug-in is installed and the samples imported, from Project Settings select XR Plug-in Management, install the plug-in if it is not, and enable the Oculus Plug-in Provider.

In Project Settings -> Player provide the following information:
Minor changes need to be done to the quality settings as well. When the URP is used, some quality settings are bypassed by a Universal Render Pipeline Asset. It is possible to create a new Universal Render Pipeline Asset, or modify the UniversalRP-MediumQuality (the default used by Android).

Select the asset and, from the Inspector, change the Anti Aliasing MSAA from Disable to 4x. From this window is possible to improve the quality of the shadows by changing the Shadows Max Distance to a lower value.
Create a new Empty scene and add:
position (0,0,0)position (0,1,1) | scale (0.25,0.25,0.25)Create a new GameObject XR -> XR Rig(Action-Based). This GameObject is the digital representation of the Oculus Headset and its controllers in the virtual scene.
Change the Tracking Origin Mode to Floor. This parameter is used to set the right height of the headset based on the Oculus calibration of the Guardian.
With the XR Rig in the scene it is already possible to build a VR experience and walk around the environment, however, the controllers are not yet in place and properly linked to the input control system.
On the XR Controller component of each hand, there is a field Model Prefab that is used to control the mesh of the VR controllers.
The Oculus Quest2 controllers, as the controllers of other VR HMD, are not provided with the XR Integration Plugin but they can be downloaded and manually installed. These are the steps to use the Quest2 controllers:
Quest2 Controllers contains three versions of the controllers, each one with a different resolution. We need to import in Unity the model quest2_controllers_div1 and the texture nextControllerBoth_color1k.png;QuestControllers and apply the nextControllerBoth_color1k.png texture as Base Map;Left_quest2_controller and Right_quest2_controllerquest2_controllers_div1, drag and drop the left_quest2_mesh and right_quest2_mesh as well as the material created
XR Controller components of each hand.The controllers are in place, but they need to be linked to the interaction system. We are going to use the Samples imported from the XR Interaction Toolkit:
Default Input Actionsin the project tab;XRI Default Left Controller and XRI Default Right Controller select Add to ActionBasedController default;
Edit -> Project Settings -> Preset Manager set the filter to left and right respectively;
XR Interaction Manager GameObject a new component Input Action Manager. The Action Assets Element 0 needs to refer to the XRI Default Input Actions
XR Controller components of both left and right hands, activate the Position Action and the Rotation Action pointing at the Position and Rotation Input Action References.
From File -> Build Settings and click on Add Open Scenes. With the Oculus Quest2 connected to your computer, press Build and Run, select the local destination folder for the APK.

The APK is installed on the Quest2. You should be able to see the Quest2 controllers tracked with a red line coming out from them. In the next part we will see how to use the controller to interact with the object in the scene.
To interact with the virtual environment, we need to attach the interactable components of the XR Interaction Toolkit to the digital objects and to the controllers themselves. Both controllers of the XR Rig prefab have already a XR Ray Interactor component attached (the red line). We are going to transform the Right controller in a direct interaction control to grab the digital objects in the scene.
XRGrabInteractable component to it. Change the Movement type to Kinematic and the Collision Detection of the Rigidbody to Continuous Dynamic. Create a new Layer called grabbable and set it to the cube.
XRInteractorLineVisual component, the LineRenderer component and the XRRayInteractor component;Select Action with ReferenceXRI RightHand/Select;XR Direct Interactor, set an Haptic EventOn Select Entered to 0.5 and duration of 0.5 seconds;radius 0.2 and IsTrigger enabled;XR Ray Interactor, disable the Force Grab.Create two new GameObjects cubes in the scene with the following parameters and name:
position (0,0.25,-1) | scale (2,0.5,0.5)position (0,0.25,-3) | scale (0.5,0.5,0.5)Create a different materials for each of them.

Build the current scene to test the environment. It is now possible to interact with the red cube: with the right hand by touching it and pressing the grip button on the side of the controller; with the left controller by pointing at the cube and pressing and holding the grip button.
Create a new GameObject XR -> Locomotion System and add a new GameObject to the scene XR -> Teleportation Area in position (0,0,0). To avoiding the overlapping, we can now delete, or disable, the Plane create at the beginning of the workshop.
The teleportation system is already working, however, the default straight line render and the key behaviour are not the most effective solutions. Moreover, we want to use both hands to teleport and interact with the objects.

We are going to create two new parent GameObjects for both hands and, for each of them, we are going to add a grab controller and a teleport controller:
LeftHand and RightHand inside the Camera Offset GameObject of the XR Rig;RightHand Controller and the LeftHand Controller in the new parents GameObjects;RightHand Controller has already the direct interaction setup, we need to add a teleportation controller. Create a new empty GameObject in the RighHand parent named RightHand Teleporter;XR Controller component from the RightHand Controller to the RightHand Teleporter;XR Ray interactor component, the Line Renderer component and the XR Interactor Line Visual component to the RightHand Teleporter (these three components can be copied from the previously created LeftHand Controller as well or added as new components);XR Controller of the RightHand Teleporter and deactivate the XR Ray interactor component just added.
XR Controller of the RightHand Teleporter, change the Selection Action -> Reference to XRI RightHand/Teleport Mode Activate and in the XR Ray interactor component to the RightHand Teleporter disable the Keep Selected Target ValidThe XR Rig should have this structure now:

Create a new C# script TeleportationManager :
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;
public class TeleportationManager : MonoBehaviour
{
public GameObject baseController; //the Right / Left Hand Controller GameObject
public GameObject teleportationController; //the Right / Left Teleport Controller GameObject
public InputActionReference teleportActivationReference; //The `XRI RightHand/Teleport Mode Activate` / `XRI LeftHand/Teleport Mode Activate`
public UnityEvent onTeleportationActive;
public UnityEvent onTeleportationCancel;
private void Start()
{
teleportActivationReference.action.performed += TeleportModeActivate;
teleportActivationReference.action.canceled += TeleportModeCancel;
}
private void TeleportModeCancel(InputAction.CallbackContext context) => Invoke("DeactivateTeleporter", 0.1f);
void DeactivateTeleporter() => onTeleportationCancel.Invoke();
private void TeleportModeActivate(InputAction.CallbackContext context) => onTeleportationActive.Invoke();
}
Attach the script to the RightHand GameObject (it will be attached also to the LeftHand GameObject) with the following values for the public fields:
To switch between the main controller and the teleportation one, add four entries for each OnTeleportationActive and OnTeleportationCancel.
On the OnTeleportationActive add:
RightHand Controller function ActionBaseController.enableInputActions disabled;RightHand Controller function XRDirectInteractor.enable disabled;RightHand Teleporter function ActionBaseController.enableInputActions enabled;RightHand Teleporter function XRRayInteractor.enabled enabled;On the OnTeleportationCancel the values will be inversed:
RightHand Controller function ActionBaseController.enableInputActions enabled;RightHand Controller function XRDirectInteractor.enable enabled;RightHand Teleporter function ActionBaseController.enableInputActions disabled;RightHand Teleporter function XRRayInteractor.enabled disabled;
In the XR Ray Interactor of the RightHand Controller change the Line Type to Bezier Curve.

Using the XR Interaction Toolkit it is possible to provide custom functions to interact with the scene. In this last part of the VR workshop we will link a virtual object with a MQTT broker to publish a message when the red cube is place to the podium.
Download the M2MQTT repository and copy the folders M2Mqtt and M2MqttUnity in the Assets folder of the Unity project.
Create a new C# script mqttReceiver:
/*
The MIT License (MIT)
Copyright (c) 2018 Giovanni Paolo Vigano'
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections.Generic;
using UnityEngine;
using M2MqttUnity;
using uPLibrary.Networking.M2Mqtt.Messages;
public class mqttReceiver : M2MqttUnityClient
{
[Header("MQTT topics")]
[Tooltip("Set the topic to subscribe. !!!ATTENTION!!! multi-level wildcard # subscribes to all topics")]
public string topicSubscribe = "#"; // topic to subscribe. !!! The multi-level wildcard # is used to subscribe to all the topics. Attention i if #, subscribe to all topics. Attention if MQTT is on data plan
[Tooltip("Set the topic to publish (optional)")]
public string topicPublish = ""; // topic to publish
public string messagePublish = ""; // topic to publish
[Tooltip("Set this to true to perform a testing cycle automatically on startup")]
public bool autoTest = false;
//using C# Property GET/SET and event listener to reduce Update overhead in the controlled objects
private string m_msg;
public string msg
{
get
{
return m_msg;
}
set
{
if (m_msg == value) return;
m_msg = value;
if (OnMessageArrived != null)
{
OnMessageArrived(m_msg);
}
}
}
public event OnMessageArrivedDelegate OnMessageArrived;
public delegate void OnMessageArrivedDelegate(string newMsg);
//using C# Property GET/SET and event listener to expose the connection status
private bool m_isConnected;
public bool isConnected
{
get
{
return m_isConnected;
}
set
{
if (m_isConnected == value) return;
m_isConnected = value;
if (OnConnectionSucceeded != null)
{
OnConnectionSucceeded(isConnected);
}
}
}
public event OnConnectionSucceededDelegate OnConnectionSucceeded;
public delegate void OnConnectionSucceededDelegate(bool isConnected);
// a list to store the messages
private List<string> eventMessages = new List<string>();
public void Publish()
{
client.Publish(topicPublish, System.Text.Encoding.UTF8.GetBytes(messagePublish), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
Debug.Log("Message published");
}
public void SetEncrypted(bool isEncrypted) => this.isEncrypted = isEncrypted;
protected override void OnConnecting() => base.OnConnecting();
protected override void OnConnected()
{
base.OnConnected();
isConnected = true;
if (autoTest) { Publish(); }
}
protected override void OnConnectionFailed(string errorMessage)
{
Debug.Log("CONNECTION FAILED! " + errorMessage);
}
protected override void OnDisconnected()
{
Debug.Log("Disconnected.");
isConnected = false;
}
protected override void OnConnectionLost() => Debug.Log("CONNECTION LOST!");
protected override void SubscribeTopics() => client.Subscribe(new string[] { topicSubscribe }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
protected override void UnsubscribeTopics() => client.Unsubscribe(new string[] { topicSubscribe });
protected override void Start() => base.Start();
protected override void DecodeMessage(string topic, byte[] message)
{
//The message is decoded
msg = System.Text.Encoding.UTF8.GetString(message);
Debug.Log("Received: " + msg);
StoreMessage(msg);
if (topic == topicSubscribe)
{
if (autoTest)
{
autoTest = false;
Disconnect();
}
}
}
private void StoreMessage(string eventMsg)
{
if (eventMessages.Count > 50)
{
eventMessages.Clear();
}
eventMessages.Add(eventMsg);
}
protected override void Update() => base.Update(); // call ProcessMqttEvents()
private void OnDestroy() => Disconnect();
private void OnValidate()
{
if (autoTest)
{
autoConnect = true;
}
}
}
Create a new C# script mqttPublisher
using UnityEngine;
[RequireComponent(typeof(mqttReceiver))]
public class mqttPublisher : MonoBehaviour
{
public mqttReceiver _eventSender;
public string messagePublish = "";
void Start()
{
_eventSender = this.gameObject.GetComponent<mqttReceiver>();
_eventSender.OnConnectionSucceeded += OnConnectionSucceededHandler;
}
private void OnConnectionSucceededHandler(bool connected)
{
if (!connected) //publish if connected
return;
//if the messagePublish is null, use the one of the MQTTReceiver
if (messagePublish.Length > 0)
{ _eventSender.messagePublish = messagePublish; }
_eventSender.Publish();
Debug.Log("Publish" + messagePublish);
}
}
Create a new C# script mqttController:
using UnityEngine;
[RequireComponent(typeof(mqttReceiver))]
public class mqttController : MonoBehaviour
{
public GameObject objectToControl;
public mqttReceiver _eventSender;
void Start()
{
_eventSender = this.gameObject.GetComponent<mqttReceiver>();
_eventSender.OnMessageArrived += OnMessageArrivedHandler;
}
private void OnMessageArrivedHandler(string newMsg)
{
Debug.Log("Event Fired. The message is = " + newMsg);
}
}
Attach the three scripts to the Red Cube GameObject:
MQTTReceiver (i.e. Broker Address, Broker Port, Topic Subscribe, Topic Publish)MQTTPublisher.
Select the Podium GameObject from the Hierarchy and add a XRSocketInteractor component. This component will link the Red Cube GameObject and trigger both connection and publishing to the MQTT Broker:
RedCube (e.g. grabbable);Podium with position (0,0.75,0) and set this object as Attach Transform of the XRSocketInteractor component (the centre of this GameObject is going to be the attach point of the RedCube);RedCube and set the function mqttReceiver.ConnectRedCube and set the function mqttReceiver.DisconnectFinally, set the Box Collider of the GameObject IsTrigger and add a RigidBodyKinematic and freeze X, Y and Z of both position and rotation

Build and Run the project, it will be now possible to grab the RedCube, teleport to the Podium and trigger the MQTT connection by adding the RedCube to the Podium.