Hello XR Developers! Today, we’re diving into a powerful new way to create colocated experiences using Shared Spatial Anchors and Colocation Discovery with Meta’s latest XR Core SDK. Starting with version 71, Meta has simplified the process significantly. In this tutorial, we’ll guide you step-by-step through the setup and explain how to use these tools to build multiplayer XR applications that align user spaces seamlessly. Let’s get started!
Getting Familiar with the Basics
Before diving into the implementation, it’s essential to understand how spatial anchors work and how they are shared. Here are some key resources to explore:
- Meta Developers: Unity Spatial Anchors and Persistent Content
- Meta Developers: Unity Shared Spatial Anchors
- Meta Developers: Unity Colocation Discovery
For more extensive samples, visit:
Note: Some examples may not yet use Colocation Discovery or group-based anchors.
The Shift from User-Based to Group-Based Sharing
In earlier versions of the Meta SDK, sharing spatial anchors required user-based permissions, which created challenges such as obtaining Oculus user IDs and configuring Quest Store apps. With SDK v71, this process has been streamlined through group-based sharing:
- No more need for user ID permissions.
- Group-based sharing uses a unique group UUID that all clients reference, simplifying anchor sharing and loading logic.
This improvement reduces setup complexity and makes it easier to develop colocated experiences.
Prerequisites
To follow this tutorial, you’ll need:
- A Meta Quest 2, Quest Pro, or Quest 3.
- Meta XR Core SDK v71 or later.
Ensure you enable Enhanced Spatial Services on your device:
- Go to Settings > Privacy and Safety > Device Permissions.
- Select Enhanced Spatial Services.
Also, set Shared Spatial Anchor Support to “required” in your OVR Manager settings.
Colocation Discovery Overview
Colocation Discovery enables devices to advertise and discover sessions using Bluetooth. Two key concepts are:
- Advertisement: One device advertises a session with a unique UUID.
- Discovery: Other devices discover the advertised session and retrieve the UUID.
No entitlement checks are needed for sharing or loading anchors when using Colocation Discovery.
Setting Up Colocation in Code
The setup requires only about 200 lines of code. We’ll use two main scripts: ColocationManager.cs
and AlignmentManager.cs
.
ColocationManager.cs
This script handles:
- Creating and sharing anchors.
- Advertising and discovering sessions.
It uses the OVRColocationSession
API and Meta’s new async/await methods, including ShareAsync
and SaveAnchorAsync
. Here’s a breakdown:
- Advertising a Session:
- The host creates a byte array containing the session name.
- It advertises the session and generates a UUID for group sharing.
- Creating an Alignment Anchor:
- An anchor is created at
Vector3.zero
. - The anchor is saved and shared using the UUID from the advertisement.
- An anchor is created at
- Discovery and Alignment:
- Clients discover advertised sessions and retrieve the group UUID.
- They load shared anchors and align their tracking space using
AlignmentManager
.
AlignmentManager.cs
This script aligns user spaces based on the shared anchor:
- It adjusts the camera rig’s position and rotation.
- Uses a double-pass system for precise alignment:
- Resets the camera rig.
- Recalculates position and rotation based on the anchor’s inverse transform.
Setting Up the Scene
- Add a camera rig and passthrough building block.
- Use the Networked Grabbable Object block to automatically set up Fusion.
- Optionally, add a Player Nametag block for visualization (requires Meta Platform SDK).
- Attach your
ColocationManager
andAlignmentManager
scripts.
Testing and Debugging
Connect your devices to your computer and use the Android Logcat package for real-time debugging:
- Filter logs to display only Unity logs.
- Observe session advertisement, anchor creation, sharing, and discovery.
When testing, remember to stop session advertisement once all clients have joined to optimize performance.
Conclusion
With Meta’s updated SDK, creating colocated experiences has never been easier. By leveraging group-based Shared Spatial Anchors and Colocation Discovery, you can eliminate the complexities of user-based permissions and build more engaging, social VR applications.
If you found this guide helpful, leave a like or subscribe! Consider supporting us on Patreon for access to full source code and more exclusive content. Don’t forget to join our XR developer community on Discord. See you in the next tutorial!
Support Black Whale🐋
Thank you for following this article. Stay tuned for more in-depth and insightful content in the realm of XR development! 🌐
Did this article help you out? Consider supporting me on Patreon, where you can find all the source code, or simply subscribe to my YouTube channel!