瞭解如何在自己的應用程式中使用 Instant Placement API 或永久射線。
必要條件
請務必先瞭解基本 AR 概念,以及如何設定 ARCore 工作階段,然後再繼續操作。
概念名稱
AR Foundation 與 Unity 的 ARCore SDK 的特定概念和方法名稱可能不同。請參閱下表,方便日後參考。
必要條件
本指南假設您已經安裝並設定 Unity。如果沒有,請參閱「開始使用適用於 AR Foundation 的 ARCore 擴充功能」一文,瞭解安裝和設定步驟。
使用 ARRaycastManager
設定新的工作階段
透過 AR Foundationation 套件,立即可用的立即位置顯示方式 (永久性光線轉換)。請按照下列步驟設定場景。
AR Foundation 4.x
新增預先定義的遊戲物件 AR Session Origin 和 AR Session。
將 AR Raycast Manager 元件新增至 AR Session Origin 遊戲物件。
當 Raycast Prefab 並非空值時,ARRaycastManager
會將 Prefab 執行個體化,並以 ARRaycast
的姿勢自動同步其姿勢。
AR Foundation 5.x
新增預先定義的遊戲物件 XR Origin 和 AR Session。
將 AR Raycast Manager 元件新增至 XR Origin 遊戲物件。
當 Raycast Prefab 並非空值時,ARRaycastManager
會將 Prefab 執行個體化,並以 ARRaycast
的姿勢自動同步其姿勢。
放置物件
在新的 ARCore 工作階段中,使用 ARRaycastManager.AddRaycast(Vector2, float)
執行命中測試。
public ARRaycastManager RaycastManager; // set from the Editor Inspector.
void Update()
{
Touch touch;
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
return;
}
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
return;
}
ARRaycast raycast = RaycastManager.AddRaycast(touch.position, _estimateDistance);
if (raycast != null)
{
// You can instantiate a 3D object here if you haven’t set Raycast Prefab in the scene.
…
}
}
監控 ARRaycast 追蹤狀態
如果 ARCore 有正確的 3D 姿勢,ARRaycast.trackingState
就會是 Tracking
。
否則,會從 Limited
開始,並在 ARCore 有準確的 3D 姿勢後轉換為 Tracking
。追蹤狀態變成 Tracking
後,不會還原為 Limited
。