Persistence scene hold is a key feature of the HoloLens holographic experience, when the user leaves the original scene, the holographic objects in the original scene will remain in a certain position, and when the user returns to the original scene, the holographic content of the original scene can be accurately restored. The Worldanchorstore class is the key API for implementing this feature, which ensures that users can paste any holographic object into any location they want to place.
How to persist holograms across sessions maintain holographic objects throughout the session
Worldanchorstore allows you to maintain the position of the space anchor in the scene, so that you can really keep the holographic object, you need to use a specific space anchor to track each object individually. Typically, a root gameobject is created with a space anchor attached, and a space anchor component with a relative position offset is attached to its sub-gameobject.
To load a holographic object from a previous scene:
- Get Worldanchorstore Object
- Loads the space anchor associated application data from which to get the spatial anchor ID
- Get the spatial anchor object by ID
Before the next scene, in order to save the holographic information:
- Get Worldanchorstore Object
- Specify the ID to hold the corresponding space anchor object
- Maintain application data associated with a spatial anchor
Getting the Worldanchorstore gets the Worldanchorstore object
Namespaces: UnityEngine.WSA.VR.Persistence
Type: worldanchorstore
In order to be able to use worldanchorstore later, we need to open this object through an asynchronous operation, as follows:
Worldanchorstore.getasync (storeloaded); Private void storeloaded (Worldanchorstore store) { this. store = store;}
You can now use the Worldanchorstore object to save the scene information.
Saving a Worldanchor save a space anchor
Note: The same spatial anchor cannot be saved multiple times. Each space anchor should be saved only once, if you need to update the space anchor information, you need to delete the old space anchor information, and then save the new space anchor information.
Private void savegame () { // Save the spatial anchor corresponding to the holographic object data if (! this// only holds the root object once { this. Store. Save ("rootgameobject", anchor); Assert (this. savedroot);} }
Loading a worldanchor loads a space anchor
Loading spatial anchor data is simple, as follows:
Private void Loadgame () { // Save data about holograms positionedby this world anchor this
this. Store. Load ("rootgameobject", Rootgameobject); if (! This . Savedroot) { // We did not successfully save the space anchor and holographic object data, we need to replace our object }}
Enumerating Existing Anchors enumerate existing space anchors
We can find the previously saved space anchors by enumerating the saved space anchor information:
string This . Store. Getallids (); for (int0; index < IDs. Length; index++) { Debug.Log (Ids[index]);}
HoloLens Development Notes-Unity's persistence scene hold