using System;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
#if UNITY_IOS
using UnityEngine.XR.ARKit;
#endif
///
/// This example shows how to activate the [ARCoachingOverlayView](https://developer.apple.com/documentation/arkit/arcoachingoverlayview)
///
[RequireComponent(typeof(ARSession))]
public class ARKitCoachingOverlay : MonoBehaviour
{
// Duplicate the ARCoachingGoal enum so that we can use it on a serialized field
enum CoachingGoal
{
Tracking,
HorizontalPlane,
VerticalPlane,
AnyPlane
}
[SerializeField]
[Tooltip("The coaching goal associated with the coaching overlay.")]
#if !UNITY_IOS
#pragma warning disable CS0414
#endif
CoachingGoal m_Goal = CoachingGoal.Tracking;
#if !UNITY_IOS
#pragma warning restore CS0414
#endif
#if UNITY_IOS
///
/// The [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal) associated with the coaching overlay
///
public ARCoachingGoal goal
{
get
{
if (GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
return sessionSubsystem.coachingGoal;
}
else
{
return (ARCoachingGoal)m_Goal;
}
}
set
{
m_Goal = (CoachingGoal)value;
if (supported && GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.coachingGoal = value;
}
}
}
#endif
[SerializeField]
[Tooltip("Whether the coaching overlay activates automatically.")]
bool m_ActivatesAutomatically = true;
///
/// Whether the coaching overlay activates automatically
///
public bool activatesAutomatically
{
get
{
#if UNITY_IOS
if (supported && GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
return sessionSubsystem.coachingActivatesAutomatically;
}
#endif
return m_ActivatesAutomatically;
}
set
{
m_ActivatesAutomatically = value;
#if UNITY_IOS
if (supported && GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.coachingActivatesAutomatically = value;
}
#endif
}
}
///
/// Whether the [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal) is supported.
///
public bool supported
{
get
{
#if UNITY_IOS
return ARKitSessionSubsystem.coachingOverlaySupported;
#else
return false;
#endif
}
}
void OnEnable()
{
#if UNITY_IOS
if (supported && GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.coachingGoal = (ARCoachingGoal)m_Goal;
sessionSubsystem.coachingActivatesAutomatically = m_ActivatesAutomatically;
}
else
#endif
{
Debug.LogError("ARCoachingOverlayView is not supported by this device.");
}
}
///
/// Activates the [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal)
///
/// If true, the coaching overlay is animated, e.g. fades in. If false, the coaching overlay appears instantly, without any transition.
public void ActivateCoaching(bool animated)
{
#if UNITY_IOS
if (supported && GetComponent().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.SetCoachingActive(true, animated ? ARCoachingOverlayTransition.Animated : ARCoachingOverlayTransition.Instant);
}
else
#endif
{
throw new NotSupportedException("ARCoachingOverlay is not supported");
}
}
}