Unity 中的追蹤遺失Tracking loss in Unity

當裝置無法在世界中找到自己的裝置時,應用程式會遇到「追蹤遺失」。When the device can't locate itself in the world, the app experiences "tracking loss". 根據預設,Unity 將會暫停更新迴圈,並在每次追蹤遺失時向使用者顯示啟動顯示映射。By default, Unity will pause the update loop and display a splash image to the user anytime tracking is lost. 一旦重新開機追蹤之後,啟動顯示映射就會消失,而且更新迴圈會繼續進行。Once tracking is regained, the splash image goes away and the update loop continues.

或者,使用者可以退出宣告設定,以手動方式處理這項轉換。As an alternative, the user can manually handle this transition by opting out of the setting. 如果沒有執行任何動作來處理,則所有內容在追蹤遺失期間都會變成主體鎖定。All content will seem to become body locked during tracking loss if nothing is done to handle it.

預設處理Default Handling

根據預設,更新迴圈和所有訊息和事件將會在追蹤遺失期間停止。The update loop and all messages and events will stop for the duration of tracking loss by default. 如此一來,使用者就會看到一個影像。At that same time, an image will be displayed to the user. 您可以從 [編輯->設定->播放程式]、按一下 [啟動顯示影像],然後設定全像攝影追蹤損失影像來自訂此映射。You can customize this image by going to Edit->Settings->Player, clicking Splash Image, and setting the Holographic Tracking Loss image.

手動處理Manual Handling

若要手動處理追蹤遺失,您需要移至 [編輯 > 專案設定 > 播放機 > ] 通用 Windows 平臺設定] 索引標籤 > 啟動顯示映射 > Windows 全像,然後取消核取 [追蹤遺失暫停和顯示影像]。To manually handle tracking loss, you need to go to Edit > Project Settings > Player > Universal Windows Platform settings tab > Splash Image > Windows Holographic and uncheck "On Tracking Loss Pause and Show Image". 之後,您必須使用下列指定的 Api 來處理追蹤變更。After which, you need to handle tracking changes with the APIs specified below.

命名空間: UnityEngine. XRNamespace: UnityEngine.XR.WSA
類型: WorldManagerType: WorldManager

  • 全域管理員會公開事件,以偵測遺失/取得的追蹤 (WorldManager OnPositionalLocatorStateChanged) 和屬性,以查詢目前狀態 (WorldManager 的狀態) World Manager exposes an event to detect tracking lost/gained (WorldManager.OnPositionalLocatorStateChanged) and a property to query the current state (WorldManager.state)
  • 當追蹤狀態為非作用中時,即使使用者翻譯,相機也不會在虛擬環境中轉譯。When the tracking state isn't active, the camera won't appear to translate in the virtual world even as the user translates. 物件不會再對應到任何實體位置,而且會顯示為已鎖定主體。Objects will no longer correspond to any physical location and all will appear body locked.

處理您自己的追蹤變更時,您必須輪詢每個框架的狀態屬性,或處理 OnPositionalLocatorStateChanged 事件。When handling tracking changes on your own, you either need to poll for the state property each frame or handle the OnPositionalLocatorStateChanged event.


最重要的狀態是 PositionalLocatorState,這表示追蹤功能完全正常運作。The most important state is PositionalLocatorState.Active, which means tracking is fully functional. 任何其他狀態都只會導致主要攝影機的旋轉差異。Any other state will result in only rotational deltas to the main camera. 例如:For example:

void Update()
    switch (UnityEngine.XR.WSA.WorldManager.state)
        case PositionalLocatorState.Active:
            // handle active
        case PositionalLocatorState.Activating:
        case PositionalLocatorState.Inhibited:
        case PositionalLocatorState.OrientationOnly:
        case PositionalLocatorState.Unavailable:
            // only rotational information is available

處理 OnPositionalLocatorStateChanged 事件Handling the OnPositionalLocatorStateChanged event

更方便的一點是,您也可以訂閱 OnPositionalLocatorStateChanged 來處理轉換:More conveniently, you can also subscribe to OnPositionalLocatorStateChanged to handle the transitions:

void Start()
    UnityEngine.XR.WSA.WorldManager.OnPositionalLocatorStateChanged += WorldManager_OnPositionalLocatorStateChanged;

private void WorldManager_OnPositionalLocatorStateChanged(PositionalLocatorState oldState, PositionalLocatorState newState)
    if (newState == PositionalLocatorState.Active)
        // Handle becoming active
        // Handle becoming rotational only

請參閱See also