SubsystemLifecycleManager.cs
6.6 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
#if !UNITY_2019_2_OR_NEWER
using UnityEngine.Experimental;
#endif
#if USE_XR_MANAGEMENT
using UnityEngine.XR.Management;
#endif
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// A base class for subsystems whose lifetime is managed by a <c>MonoBehaviour</c>.
/// </summary>
/// <typeparam name="TSubsystem">The <c>Subsystem</c> which provides this manager data.</typeparam>
/// <typeparam name="TSubsystemDescriptor">The <c>SubsystemDescriptor</c> required to create the Subsystem.</typeparam>
public class SubsystemLifecycleManager<TSubsystem, TSubsystemDescriptor> : MonoBehaviour
where TSubsystem : Subsystem<TSubsystemDescriptor>
where TSubsystemDescriptor : SubsystemDescriptor<TSubsystem>
{
/// <summary>
/// Get the <c>TSubsystem</c> whose lifetime this component manages.
/// </summary>
public TSubsystem subsystem { get; private set; }
/// <summary>
/// The descriptor for the subsystem.
/// </summary>
/// <value>
/// The descriptor for the subsystem.
/// </value>
public TSubsystemDescriptor descriptor
{
get { return (subsystem == null) ? null : subsystem.SubsystemDescriptor; }
}
bool m_CleanupSubsystemOnDestroy = true;
/// <summary>
/// Creates a <c>TSubsystem</c>.
/// </summary>
/// <returns>The first Subsystem of matching the <c>TSubsystemDescriptor</c>, or <c>null</c> if there aren't any.</returns>
protected virtual TSubsystem CreateSubsystem()
{
SubsystemManager.GetSubsystemDescriptors(s_SubsystemDescriptors);
if (s_SubsystemDescriptors.Count > 0)
{
var descriptor = s_SubsystemDescriptors[0];
if (s_SubsystemDescriptors.Count > 1)
{
Debug.LogWarningFormat("Multiple {0} found. Using {1}",
typeof(TSubsystem).Name,
descriptor.id);
}
return descriptor.Create();
}
else
{
return null;
}
}
/// <summary>
/// Creates a subsystem if subsystem is <c>null</c>.
/// </summary>
protected void CreateSubsystemIfNecessary()
{
// Use the subsystem that has been instantiated by XR Management
// if available, otherwise create the subsystem.
if (subsystem == null)
{
subsystem = GetActiveSubsystemInstance();
// If the subsystem has already been created by XR management, it controls the lifetime
// of the subsystem.
if (subsystem != null)
m_CleanupSubsystemOnDestroy = false;
}
if (subsystem == null)
subsystem = CreateSubsystem();
}
/// <summary>
/// Returns the active <c>TSubsystem</c> instance if present, otherwise returns null.
/// </summary>
protected TSubsystem GetActiveSubsystemInstance()
{
TSubsystem activeSubsystem = null;
#if USE_XR_MANAGEMENT
// If the XR management package has been included, query the currently
// active loader for the created subsystem, if one exists.
if (XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null)
{
XRLoader loader = XRGeneralSettings.Instance.Manager.activeLoader;
if (loader != null)
activeSubsystem = loader.GetLoadedSubsystem<TSubsystem>();
}
#endif
// If XR management is not used or no loader has been set, check for
// any active subsystem instances in the SubsystemManager.
if (activeSubsystem == null)
{
SubsystemManager.GetInstances(s_SubsystemInstances);
#if !UNITY_2019_3_OR_NEWER
foreach (var instance in s_SubsystemInstances)
{
if (!s_DestroyedSubsystemTypes.Contains(instance))
{
return instance;
}
}
#else
if (s_SubsystemInstances.Count > 0)
{
activeSubsystem = s_SubsystemInstances[0];
}
#endif
}
return activeSubsystem;
}
/// <summary>
/// Creates the <c>TSubsystem</c>.
/// </summary>
protected virtual void OnEnable()
{
CreateSubsystemIfNecessary();
if (subsystem != null)
{
OnBeforeStart();
// The derived class may disable the
// component if it has invalid state
if (enabled)
{
subsystem.Start();
OnAfterStart();
}
}
}
/// <summary>
/// Stops the <c>TSubsystem</c>.
/// </summary>
protected virtual void OnDisable()
{
if (subsystem != null)
subsystem.Stop();
}
/// <summary>
/// Destroys the <c>TSubsystem</c>.
/// </summary>
protected virtual void OnDestroy()
{
if (m_CleanupSubsystemOnDestroy && subsystem != null)
{
#if !UNITY_2019_3_OR_NEWER
s_DestroyedSubsystemTypes.Add(subsystem);
#endif
subsystem.Destroy();
}
subsystem = null;
}
/// <summary>
/// Invoked after creating the subsystem and before calling Start on it.
/// The <see cref="subsystem"/> is not <c>null</c>.
/// </summary>
protected virtual void OnBeforeStart()
{ }
/// <summary>
/// Invoked after calling Start on it the Subsystem.
/// The <see cref="subsystem"/> is not <c>null</c>.
/// </summary>
protected virtual void OnAfterStart()
{ }
static List<TSubsystemDescriptor> s_SubsystemDescriptors =
new List<TSubsystemDescriptor>();
static List<TSubsystem> s_SubsystemInstances =
new List<TSubsystem>();
#if !UNITY_2019_3_OR_NEWER
static HashSet<TSubsystem> s_DestroyedSubsystemTypes =
new HashSet<TSubsystem>();
#endif
}
}