-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ARKitCoachingOverlay.cs
164 lines (152 loc) · 5.67 KB
/
ARKitCoachingOverlay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
#if UNITY_IOS
using UnityEngine.XR.ARKit;
#endif
namespace UnityEngine.XR.ARFoundation.Samples
{
/// <summary>
/// This example shows how to activate the [ARCoachingOverlayView](https://developer.apple.com/documentation/arkit/arcoachingoverlayview)
/// </summary>
[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
/// <summary>
/// The [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal) associated with the coaching overlay
/// </summary>
public ARCoachingGoal goal
{
get
{
if (GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
return sessionSubsystem.requestedCoachingGoal;
}
else
{
return (ARCoachingGoal)m_Goal;
}
}
set
{
m_Goal = (CoachingGoal)value;
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.requestedCoachingGoal = value;
}
}
}
#endif
[SerializeField]
[Tooltip("Whether the coaching overlay activates automatically.")]
bool m_ActivatesAutomatically = true;
/// <summary>
/// Whether the coaching overlay activates automatically
/// </summary>
public bool activatesAutomatically
{
get
{
#if UNITY_IOS
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
return sessionSubsystem.coachingActivatesAutomatically;
}
#endif
return m_ActivatesAutomatically;
}
set
{
m_ActivatesAutomatically = value;
#if UNITY_IOS
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.coachingActivatesAutomatically = value;
}
#endif
}
}
/// <summary>
/// Whether the [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal) is supported.
/// </summary>
public bool supported
{
get
{
#if UNITY_IOS
return ARKitSessionSubsystem.coachingOverlaySupported;
#else
return false;
#endif
}
}
void OnEnable()
{
#if UNITY_IOS
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.requestedCoachingGoal = (ARCoachingGoal)m_Goal;
sessionSubsystem.coachingActivatesAutomatically = m_ActivatesAutomatically;
sessionSubsystem.sessionDelegate = new CustomSessionDelegate();
}
else
#endif
{
Debug.LogError("ARCoachingOverlayView is not supported by this device.");
}
}
/// <summary>
/// Activates the [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal)
/// </summary>
/// <param name="animated">If <c>true</c>, the coaching overlay is animated, e.g. fades in. If <c>false</c>, the coaching overlay appears instantly, without any transition.</param>
public void ActivateCoaching(bool animated)
{
#if UNITY_IOS
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.SetCoachingActive(true, animated ? ARCoachingOverlayTransition.Animated : ARCoachingOverlayTransition.Instant);
}
else
#endif
{
throw new NotSupportedException("ARCoachingOverlay is not supported");
}
}
/// <summary>
/// Disables the [ARCoachingGoal](https://developer.apple.com/documentation/arkit/arcoachinggoal)
/// </summary>
/// <param name="animated">If <c>true</c>, the coaching overlay is animated, e.g. fades out. If <c>false</c>, the coaching overlay disappears instantly, without any transition.</param>
public void DisableCoaching(bool animated)
{
#if UNITY_IOS
if (supported && GetComponent<ARSession>().subsystem is ARKitSessionSubsystem sessionSubsystem)
{
sessionSubsystem.SetCoachingActive(false, animated ? ARCoachingOverlayTransition.Animated : ARCoachingOverlayTransition.Instant);
}
else
#endif
{
throw new NotSupportedException("ARCoachingOverlay is not supported");
}
}
}
}