AR应用时常需要根据tracking状态做出响应,比如在跟踪丢失的时候提示用户从新进行定位。这就需要AR工程内提供一个专门负责跟踪tracking状态的类。通过 ARFoundation提供的ARSession类可以获取到跟踪的状态
废话不多说,直接上代码:
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARStatusManager : MonoBehaviour
{
private void OnEnable()
{
ARSession.stateChanged += OnStateChanged;
}
private void OnDisable()
{
ARSession.stateChanged -= OnStateChanged;
}
private void OnStateChanged(ARSessionStateChangedEventArgs args)
{
switch (args.state)
{
case ARSessionState.None:
break;
case ARSessionState.CheckingAvailability:
break;
case ARSessionState.Installing:
break;
case ARSessionState.NeedsInstall:
break;
case ARSessionState.Ready:
break;
case ARSessionState.SessionInitializing:
break;
case ARSessionState.SessionTracking:
break;
case ARSessionState.Unsupported:
break;
}
switch (ARSession.notTrackingReason)
{
case NotTrackingReason.None:
break;
case NotTrackingReason.Initializing:
break;
case NotTrackingReason.Relocalizing:
break;
case NotTrackingReason.InsufficientLight:
break;
case NotTrackingReason.InsufficientFeatures:
break;
case NotTrackingReason.ExcessiveMotion:
break;
case NotTrackingReason.Unsupported:
break;
}
}
}
网友评论