title | description | author | ms.author | ms.date | ms.topic | keywords |
---|---|---|---|---|---|---|
Persistence in Unity |
Use the Unity WorldAnchorStore persistence feature in older Unity versions to let users pin holograms and find them later over app sessions. |
thetuvix |
alexturn |
05/03/2022 |
article |
HoloLens, persistence, Unity, mixed reality headset, windows mixed reality headset, virtual reality headset |
When possible, use World Locking Tools for all your hologram positioning needs. For Unity 2019/2020 using OpenXR or the Windows XR Plugin, use ARAnchorManager
. For more information, see Choose your world locking approach.
For older Unity versions or WSA projects, the WorldAnchorStore
creates holographic experiences where holograms stay in specific real-world positions across application instances. Users can pin individual holograms wherever they want, and find them in the same spot over many app sessions.
Namespace: UnityEngine.XR.WSA.Persistence
Class: WorldAnchorStore
The WorldAnchorStore
lets you persist the location of world anchors across sessions. To persist holograms across sessions, keep separate track of GameObjects
that use a particular world anchor. You can create a GameObject
root with a world anchor, and anchor child holograms by it with a local position offset.
To load holograms from previous sessions:
- Get the
WorldAnchorStore
. - Load world anchor app data, which gives you the ID of the world anchor.
- Load the world anchor by its ID.
To save holograms for future sessions:
- Get the
WorldAnchorStore
. - Save a world anchor, specifying an ID.
- Save app data related to the world anchor along with the ID.
Keep a reference to the WorldAnchorStore
, so you know when it's ready to perform an operation. Since this call is asynchronous, as soon as the app starts up you can call:
WorldAnchorStore.GetAsync(StoreLoaded);
StoreLoaded
is the handler when the WorldAnchorStore
finishes loading:
private void StoreLoaded(WorldAnchorStore store)
{
this.store = store;
}
You now have a reference to the WorldAnchorStore
, which you can use to save and load specific world anchors.
To save a world anchor, name the world anchor and pass it in the WorldAnchorStore
you got before. If you try to save two anchors to the same string, store.Save
returns false. Delete the previous save before saving a new one.
private void SaveGame()
{
// Save data about holograms that this world anchor positions
if (!this.savedRoot) // Only save the root once
{
this.savedRoot = this.store.Save("rootGameObject", anchor);
Assert(this.savedRoot);
}
}
To load a world anchor:
private void LoadGame()
{
// Saved data about holograms that this world anchor positions:
this.savedRoot = this.store.Load("rootGameObject", rootGameObject);
if (!this.savedRoot)
{
// Game root not saved. Re-place objects or start over.
}
}
You can also use store.Delete()
to remove an anchor you previously saved, and store.Clear()
to remove all previously saved data.
To list stored anchors, call GetAllIds
.
string[] ids = this.store.GetAllIds();
for (int index = 0; index < ids.Length; index++)
{
Debug.Log(ids[index]);
}
You can use Azure Spatial Anchors to create a durable cloud anchor from a local world anchor. Your app can locate the cloud anchor across multiple HoloLens, iOS, and Android devices, even if the devices aren't together at the same time. Because cloud anchors are persistent, multiple devices can see content rendered relative to that anchor in the same physical location over time.
To get started building shared experiences in Unity, try out the five-minute Azure Spatial Anchors Unity quickstarts. Once you're up and running with Azure Spatial Anchors, you can create and locate anchors in Unity.
Continue on with the Unity development checkpoints.
Explore other Mixed Reality core building blocks:
[!div class="nextstepaction"] Spatial mapping
Start exploring the Mixed Reality platform capabilities and APIs:
[!div class="nextstepaction"] Shared experiences