Unity 中的持續性Persistence in Unity

命名空間: UnityEngine. XRNamespace: UnityEngine.XR.WSA.Persistence
類別: WorldAnchorStoreClass: WorldAnchorStore

WorldAnchorStore 是建立全像攝影體驗的關鍵,也就是在應用程式的各個實例上,全像地方都有特定的真實世界位置。The WorldAnchorStore is the key to creating holographic experiences where holograms stay in specific real world positions across instances of the application. 然後,使用者可以在想要的任何地方釘選個別的全像Users can then pin individual holograms wherever they want, and find them later in the same spot over many uses of your app.

如何跨會話保存全息How to persist holograms across sessions

WorldAnchorStore 可讓您跨會話保存 WorldAnchor 的位置。The WorldAnchorStore will allow you to persist the location of WorldAnchor's across sessions. 若要實際跨會話保存全像投影,您必須個別追蹤使用特定世界錨點的 Gameobject。To actually persist holograms across sessions, you'll need to separately keep track of your GameObjects that use a particular world anchor. 建立具有世界錨點的 GameObject 根目錄通常是合理的,並讓子影像以本機位置位移錨定。It often makes sense to create a GameObject root with a world anchor and have children holograms anchored by it with a local position offset.

若要從先前的會話載入全息影像:To load holograms from previous sessions:

  1. 取得 WorldAnchorStoreGet the WorldAnchorStore
  2. 載入與世界錨點相關的應用程式資料,以提供您世界錨點的識別碼Load app data relating to the world anchor, which gives you the ID of the world anchor
  3. 從識別碼載入世界錨點Load a world anchor from its ID

若要在未來的會話中儲存影像:To save holograms for future sessions:

  1. 取得 WorldAnchorStoreGet the WorldAnchorStore
  2. 儲存指定識別碼的世界錨點Save a world anchor specifying an ID
  3. 儲存與世界錨點相關的應用程式資料以及識別碼Save app data relating to the world anchor along with an ID

取得 WorldAnchorStoreGetting the WorldAnchorStore

您會想要保留 WorldAnchorStore 的參考,讓您知道它準備好執行作業的時間。You'll want to keep a reference to the WorldAnchorStore so you know when it's ready to perform an operation. 因為這是非同步呼叫,可能會在啟動時立即執行,您想要呼叫:Since this is an async call, potentially as soon as start up, you want to call:


StoreLoaded 在此案例中是 WorldAnchorStore 完成載入時的處理常式:StoreLoaded in this case is our handler for when the WorldAnchorStore has completed loading:

private void StoreLoaded(WorldAnchorStore store)
       this.store = store;

我們現在已有 WorldAnchorStore 的參考,我們將用它來儲存和載入特定的世界錨點。We now have a reference to the WorldAnchorStore, which we'll use to save and load specific World Anchors.

儲存 WorldAnchorSaving a WorldAnchor

為了節省時間,我們只需要命名要儲存的內容,並將它傳遞到我們要儲存的 WorldAnchor 中。To save, we simply need to name what we are saving and pass it in the WorldAnchor we got before when we want to save. 注意:嘗試將兩個錨點儲存至相同的字串將會 (存放區失敗。Save 會傳回 false) 。Note: trying to save two anchors to the same string will fail (store.Save will return false). 儲存新的儲存之前,請先刪除先前的儲存:Delete the previous save before saving the new one:

private void SaveGame()
       // Save data about holograms positioned by this world anchor
       if (!this.savedRoot) // Only Save the root once
              this.savedRoot = this.store.Save("rootGameObject", anchor);

載入 WorldAnchorLoading a WorldAnchor

並載入:And to load:

private void LoadGame()
       // Save data about holograms positioned by this world anchor
       this.savedRoot = this.store.Load("rootGameObject", rootGameObject);
       if (!this.savedRoot)
              // We didn't actually have the game root saved! We have to re-place our objects or start over

此外,我們還可以使用 store。刪除 ( # A1 以移除先前儲存和儲存的錨點。清除 ( # A3 以移除所有先前儲存的資料。We additionally can use store.Delete() to remove an anchor we previously saved and store.Clear() to remove all previously saved data.

列舉現有錨點Enumerating Existing Anchors

若要探索先前儲存的錨點,請呼叫 GetAllIds。To discover previously stored anchors, call GetAllIds.

string[] ids = this.store.GetAllIds();
for (int index = 0; index < ids.Length; index++)

保存多個裝置的全像影像Persisting holograms for multiple devices

您可以使用 Azure 空間錨點 ,從本機 WorldAnchor 建立持久的雲端錨點,讓您的應用程式可以在多個 HoloLens、IOS 和 Android 裝置上找到,即使這些裝置不會同時出現在同一時間。You can use Azure Spatial Anchors to create a durable cloud anchor from a local WorldAnchor, which your app can then locate across multiple HoloLens, iOS and Android devices, even if those devices aren't present together at the same time. 由於雲端錨點是持續性的,因此每一段時間的多個裝置都可以看到相對於相同實體位置中錨點轉譯的內容。Because cloud anchors are persistent, multiple devices over time can each see content rendered relative to that anchor in the same physical location.

若要開始在 Unity 中建立共用體驗,請嘗試5分鐘的 Azure 空間錨點 Unity 快速入門To get started building shared experiences in Unity, try out the 5-minute Azure Spatial Anchors Unity quickstarts.

在您啟動並執行 Azure 空間錨點之後,您可以 在 Unity 中建立及尋找錨點Once you're up and running with Azure Spatial Anchors, you can then create and locate anchors in Unity.

下一個開發檢查點Next Development Checkpoint

如果您正在遵循我們所配置的 Unity 開發檢查點旅程,您將會在探索混合現實核心構成要素。If you're following the Unity development checkpoint journey we've laid out, you're in the midst of exploring the Mixed Reality core building blocks. 接下來,您可以繼續進行下一個建置組塊:From here, you can continue to the next building block:

或者,直接跳到混合實境平台功能和 API 的主題:Or jump to Mixed Reality platform capabilities and APIs:

您可以隨時回到 Unity 開發檢查點You can always go back to the Unity development checkpoints at any time.

另請參閱See Also