Spatial Anchors in Unreal
Spatial anchors are used to save holograms in real-world space between application sessions. These get surfaced through Unreal as ARPins and saved in the HoloLens’ anchor store, which is loaded in future sessions.
Checking the anchor store
Before saving or loading anchors, you need to check if the anchor store is ready. Calling any of the HoloLens anchor functions before the anchor store is ready will not succeed.
Once the application has a component that needs to be pinned to the world, it can be saved to the anchor store with the following sequence:
Breaking this down:
Spawn an actor at a known location.
Create an ARPin with that location and a name based on the actor’s class.
Add the actor to the ARPin and save the pin to the HoloLens anchor store.
- The anchor name you choose must be unique, which in this example is the current timestamp.
If the anchor is successfully saved to the anchor store, you can be inspect it in the HoloLens device portal under System > Map manager > Anchor Files Saved On Device.
When an application starts, you can use the following blueprint to restore components to their anchor locations:
Breaking this down:
Iterate over all of the anchors in the anchor store.
Spawn an actor at identity.
Pin that actor to the ARPin from the anchor store.
- It's important to spawn the actor at identity since the anchor is responsible for repositioning the hologram in the world based on where it was saved. Any transform added here will add an offset to the anchor.
The anchor ID is also queried so that different actors can be spawned depending on the anchor’s saved name.
When you're done with an anchor you can clear individual anchors or the entire anchor store with the Remove ARPin from WMRAnchor Store and Remove All ARPins from WMRAnchor Store components.
Bear in mind that Spatial Anchors are still in Beta, so be sure to check back for updated information and features.