Convert Figma logo to code with AI

Unity-Technologies logoXR-Interaction-Toolkit-Examples

This repository contains various examples to use with the XR Interaction Toolkit

1,086
351
1,086
79

Top Related Projects

This repository is for the legacy Mixed Reality Toolkit (MRTK) v2. For the latest version of the MRTK please visit https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity

SteamVR Unity Plugin - Documentation at: https://valvesoftware.github.io/steamvr_unity_plugin/

Google VR SDK for Unity

Quick Overview

The Unity-Technologies/XR-Interaction-Toolkit-Examples repository provides a collection of example scenes and scripts that demonstrate the usage of the XR Interaction Toolkit, a set of components and scripts in Unity that make it easier to build VR and AR applications. The examples cover a wide range of interaction scenarios and features of the toolkit.

Pros

  • Comprehensive Examples: The repository contains a diverse set of example scenes and scripts that cover various aspects of the XR Interaction Toolkit, making it a valuable resource for learning and exploration.
  • Active Development: The repository is actively maintained by the Unity Technologies team, ensuring that the examples stay up-to-date with the latest version of the toolkit.
  • Detailed Documentation: Each example comes with a detailed README file that explains the purpose of the example and how to use it.
  • Customizable: The examples are designed to be easily customizable, allowing developers to use them as a starting point for their own projects.

Cons

  • Dependency on Unity: The examples are specific to the Unity game engine and may not be directly applicable to other development platforms.
  • Steep Learning Curve: The XR Interaction Toolkit itself has a steep learning curve, and the examples may not be immediately accessible to developers who are new to Unity or VR/AR development.
  • Limited Scope: While the examples cover a wide range of interaction scenarios, they may not address all the specific needs of a particular project.
  • Potential Compatibility Issues: As the toolkit and Unity evolve, there may be compatibility issues with older versions of the examples.

Code Examples

Here are a few code examples from the repository:

  1. Grab Interactable Example:
public class GrabInteractable : MonoBehaviour, IGrabInteractable
{
    public void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
    {
        switch (updatePhase)
        {
            case XRInteractionUpdateOrder.UpdatePhase.Dynamic:
                // Update the object's position and rotation based on the interactor's movement
                transform.position = m_Interactor.transform.position;
                transform.rotation = m_Interactor.transform.rotation;
                break;
        }
    }

    public void OnSelectEntered(SelectEnterEventArgs args)
    {
        // Perform any necessary setup when the object is selected
        m_Interactor = args.interactor;
    }

    public void OnSelectExited(SelectExitEventArgs args)
    {
        // Perform any necessary cleanup when the object is deselected
        m_Interactor = null;
    }
}

This code example demonstrates how to implement a simple grab interactable object using the XR Interaction Toolkit.

  1. Teleportation Example:
public class TeleportationProvider : MonoBehaviour, ITeleportationProvider
{
    public void StartTeleportation(TeleportRequest teleportRequest)
    {
        // Perform the teleportation logic
        transform.position = teleportRequest.destinationPosition;
        transform.rotation = teleportRequest.destinationRotation;
    }

    public bool CanTeleport(TeleportRequest teleportRequest)
    {
        // Check if the teleportation request is valid
        return true;
    }
}

This code example demonstrates how to implement a simple teleportation provider using the XR Interaction Toolkit.

  1. Locomotion Example:
public class LocomotionSystem : MonoBehaviour, ILocomotionSystem
{
    public void UpdateLocomotion(LocomotionSystem.UpdatePhase updatePhase, float deltaTime)
    {
        switch (updatePhase)
        {
            case LocomotionSystem.UpdatePhase.Fixed:
                // Update the player's position and rotation based on the input
                transform.position += transform.forward * m_MoveSpeed * deltaTime;
                break;
        }
    }

    public void EnableLocomotion()
    {
        // Enable the locomotion system
        enabled = true;
    }

    public void DisableLocomotion()
    {
        // Disable the locomotion system
        enabled = false;
    }

Competitor Comparisons

This repository is for the legacy Mixed Reality Toolkit (MRTK) v2. For the latest version of the MRTK please visit https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity

Pros of MixedRealityToolkit-Unity

  • More comprehensive feature set, including advanced input systems and spatial awareness
  • Better support for Microsoft HoloLens and Windows Mixed Reality devices
  • Larger community and more frequent updates

Cons of MixedRealityToolkit-Unity

  • Steeper learning curve due to its extensive framework
  • May be overkill for simpler XR projects
  • Less flexibility for custom implementations compared to XR-Interaction-Toolkit-Examples

Code Comparison

MixedRealityToolkit-Unity:

public class GazeProvider : InputSystemGlobalHandlerListener, IMixedRealityGazeProvider
{
    [SerializeField]
    private GameObject gazeTransform = null;
    
    public override void OnSourceDetected(SourceStateEventData eventData)
    {
        // Handle gaze source detection
    }
}

XR-Interaction-Toolkit-Examples:

public class XRGazeInteractor : XRBaseInteractor
{
    [SerializeField]
    private float maxRaycastDistance = 10f;
    
    protected override void OnEnable()
    {
        base.OnEnable();
        // Set up gaze interactor
    }
}

The code snippets show different approaches to implementing gaze interaction. MixedRealityToolkit-Unity uses a more complex system with global handlers, while XR-Interaction-Toolkit-Examples offers a simpler, more modular approach.

SteamVR Unity Plugin - Documentation at: https://valvesoftware.github.io/steamvr_unity_plugin/

Pros of steamvr_unity_plugin

  • Specifically designed for SteamVR, offering deep integration with Steam's VR ecosystem
  • Provides access to advanced SteamVR features like chaperone boundaries and controller models
  • Extensive documentation and community support due to its long-standing presence in the market

Cons of steamvr_unity_plugin

  • Limited to SteamVR platform, reducing cross-platform compatibility
  • May require more setup and configuration compared to the more streamlined XR Interaction Toolkit
  • Less frequent updates and potentially slower adoption of new Unity features

Code Comparison

XR-Interaction-Toolkit-Examples:

[RequireComponent(typeof(XRGrabInteractable))]
public class XRGrabbable : MonoBehaviour
{
    private XRGrabInteractable m_GrabInteractable;
    private void Awake() => m_GrabInteractable = GetComponent<XRGrabInteractable>();
}

steamvr_unity_plugin:

[RequireComponent(typeof(SteamVR_TrackedObject))]
public class SteamVRGrabbable : MonoBehaviour
{
    private SteamVR_TrackedObject trackedObj;
    private void Awake() => trackedObj = GetComponent<SteamVR_TrackedObject>();
}

Both examples show similar component requirements and initialization, but use platform-specific classes.

Google VR SDK for Unity

Pros of gvr-unity-sdk

  • Specifically designed for Google VR (Cardboard and Daydream), offering optimized performance for these platforms
  • Includes pre-built UI elements and interactions tailored for mobile VR experiences
  • Provides a simpler setup process for developers targeting Google VR platforms

Cons of gvr-unity-sdk

  • Limited to Google VR platforms, lacking support for other XR devices and ecosystems
  • Less frequently updated compared to XR Interaction Toolkit Examples, potentially missing newer XR features
  • May have a steeper learning curve for developers not familiar with Google VR specifics

Code Comparison

XR Interaction Toolkit Examples:

[RequireComponent(typeof(XRGrabInteractable))]
public class XRGrabInteractableToAttach : MonoBehaviour
{
    private XRGrabInteractable m_GrabInteractable;
    private void Awake() => m_GrabInteractable = GetComponent<XRGrabInteractable>();
}

gvr-unity-sdk:

public class GvrReticlePointer : GvrBasePointer
{
    private GvrReticleRenderer reticle;
    protected override void Start()
    {
        base.Start();
        reticle = gameObject.GetComponent<GvrReticleRenderer>();
    }
}

Both examples showcase component-based architectures, but XR Interaction Toolkit uses more modern Unity features and a more generic approach to XR interactions, while gvr-unity-sdk focuses on Google VR-specific implementations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

XR Interaction Toolkit Examples - Version 3.0.3

Introduction

This project provides examples that use Unity's XR Interaction Toolkit (XRI) to demonstrate its functionality with example assets and behaviors. The intention of this project is to provide a means for getting started with the features in the XR Interaction Toolkit package.

Note: If you are looking for the original XRI Examples project, that has been archived into two separate branches Classic 1.0 and Classic 2.2. Both of these branches still have both the AR and VR projects available.

Getting started

Requirements

The current version of the XRI Examples is compatible with the following versions of the Unity Editor:

  • 2021.3 and later

Downloading the project

  1. Clone or download this repository to a workspace on your drive
    1. Click the ⤓ Code button on this page to get the URL to clone with Git or click Download ZIP to get a copy of this repository that you can extract
  2. Open a project in Unity
    1. Download, install, and run Unity Hub
    2. In the Installs tab, select Locate or Add to find or install Unity 2021.3 LTS or later. Include the Windows Build Support (IL2CPP) module if building for PC, and the Android Build Support if building for Android (for example, Meta Quest).
    3. In the Projects tab, click Add
    4. Browse to folder where you downloaded a copy of this repository and click Select Folder
    5. Verify the project has been added as XR-Interaction-Toolkit-Examples, and click on it to open the project

General setup

The main example scene is located at Assets/XRI_Examples/Scenes/XRI_Examples_Main. This example scene is laid out as a ring with different stations along it. The first examples you will encounter are the simplest use-cases of XRI features. Behind each example is a doorway leading to advanced uses of each feature.

Use the simple examples when you need objects you can copy-and-paste, while leveraging the advanced examples when needing to achieve complex outcomes.

The XR Origin is located within the Complete Set Up prefab. This prefab contains everything needed for a fully functional user interaction with XRI. This includes the components needed for general input, interaction, and UI interaction.

Scripts, assets, and prefabs related to each feature or use case are located in the associated folder in Assets/XRI_Examples.

The following stations are available in the XRI Examples:

For a list of new features and deprecations, see XRI Examples Changelog.

For an overview of how the Input System is used in this example, see Input.

Sharing feedback

The XR Interaction Toolkit and Input forum is the best place to open discussions and ask questions. Please use the public roadmap to submit feature requests. If you encounter a bug, please use the Unity Bug Reporter in the Unity Editor, accessible via Help > Report a Bug. Include “XR Interaction Toolkit” in the title to help our team triage things appropriately!

Contributions and pull requests

We are not accepting pull requests at this time.