page_type: sample name: QR code tracking in Unity description: This sample shows you how to use QR Codes in Unity projects using a HoloLens or Windows Mixed Reality immersive headset. languages:
Supported Unity versions | Built with XR configuration |
---|---|
Unity 2020 or higher | Windows XR Plugin |
This sample shows you how to use QR Codes in Unity projects using a HoloLens or Windows Mixed Reality immersive headset. Covered features include displaying a holographic square over QR codes and associated data such as:
File/folder | Description |
---|---|
Sample/Assets |
Unity assets, scenes, prefabs, and scripts. |
Sample/Packages |
Project manifest and packages list. |
Sample/ProjectSettings |
Unity asset setting files. |
Sample/UserSettings |
Generated user settings from Unity. |
.gitignore |
Define what to ignore at commit time. |
README.md |
This README file. |
LICENSE |
The license for the sample. |
You can find the full article on QR code tracking on Microsoft Docs. The following is a breakdown of each C# script in the sample app:
QRCode.cs
- This script is attached to the QR code object and populates the text displayed in the scene with the QR code properties on Start
.
QRCodesManager.cs
- This is the main class that handles the QR SDK, including:
QRCodesSetup.cs
- Kicks off the QR code manager tracking functionality in QRCodesManager
.
QRCodesVisualizer.cs
- Handles all QR code visualizing in the scene and instantiates all QR codes in the local list kept in QRCodesManager
.
SpatialGraphNodeTracker.cs
- This script is attached to the QR code object and transforms real-world QR code coordinates into the Unity coordinate system. The script also places the virtual QR code in the scene at the same location as the real-world QR code.
SpatialGraphNode.cs
- This script is abstracting the tracking of a spatial graph static node, which represents the tracking information of QR code in a GUID id.
Note that the "main" branch of this sample is working with Unity's "Windows XR Plugin" which works with the WinRT APIs in in Unity 2019 or 2020 LTS versions.
After upgrading to Unity 2020 or Unity 2021, You can also use "OpenXR plugin" for HoloLens 2 developement. With OpenXR plugin, the app can use the built-in support for SpatialGraphNode, and the QR code tracking will work mostly the same way as above.
To view the OpenXR version of the QRCode tracking on HoloLens 2, please checkout the "openxr" branch of this sample repro, https://github.com/microsoft/MixedReality-QRCode-Sample/tree/OpenXR.
You can also inspect how to make modifications to existing QRCode Unity project to support OpenXR from this pull request: https://github.com/microsoft/MixedReality-QRCode-Sample/pull/18
You can find the complete API reference for Microsoft.MixedReality.QR NuGet package on Microsoft Docs.
Quiet zones: To be read correctly, QR codes require a margin around all sides of the code. This margin must not contain any printed content and should be four modules (a single black square in the code) wide.
Lighting and backdrop: QR code detection quality is susceptible to varying illumination and backdrop. In a scene with bright lighting, print a code that is black on a gray background. Otherwise, print a black QR code on a white background.
Size: Windows Mixed Reality devices don't work with QR codes with sides smaller than 5 cm each. For QR codes between 5 cm and 10-cm length sides, you must be fairly close to detect the code. It will also take longer to detect codes at this size.
Distance and angular position: The tracking cameras can only detect a certain level of detail. For small codes - < 10 cm along the sides - you must be fairly close. For a version 1 QR code varying from 10 cm to 25 cm wide, the minimum detection distance ranges from 0.15 meters to 0.5 meters.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.