SubsystemLifecycleManager.cs
4.53 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
using System;
using System.Collections.Generic;
using UnityEngine.XR.Management;
#if UNITY_2020_2_OR_NEWER
using UnityEngine.SubsystemsImplementation;
#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
#if UNITY_2020_2_OR_NEWER
, TProvider
#endif
> : MonoBehaviour
#if UNITY_2020_2_OR_NEWER
where TSubsystem : SubsystemWithProvider<TSubsystem, TSubsystemDescriptor, TProvider>, new()
where TSubsystemDescriptor : SubsystemDescriptorWithProvider<TSubsystem, TProvider>
where TProvider : SubsystemProvider<TSubsystem>
#else
where TSubsystem : Subsystem<TSubsystemDescriptor>
where TSubsystemDescriptor : SubsystemDescriptor<TSubsystem>
#endif
{
/// <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 =>
#if UNITY_2020_2_OR_NEWER
subsystem?.subsystemDescriptor;
#else
subsystem?.SubsystemDescriptor;
#endif
/// <summary>
/// Returns the active <c>TSubsystem</c> instance if present, otherwise returns null.
/// </summary>
/// <returns>The active subsystem instance, or `null` if there isn't one.</returns>
protected TSubsystem GetActiveSubsystemInstance()
{
TSubsystem activeSubsystem = null;
// 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>();
}
if (activeSubsystem == null)
Debug.LogWarningFormat($"No active {typeof(TSubsystem).FullName} is available. Please ensure that a " +
"valid loader configuration exists in the XR project settings.");
return activeSubsystem;
}
/// <summary>
/// Called by derived classes to initialize the subsystem is initialized before use
/// </summary>
protected void EnsureSubsystemInstanceSet()
{
subsystem = GetActiveSubsystemInstance();
}
/// <summary>
/// Creates the <c>TSubsystem</c>.
/// </summary>
protected virtual void OnEnable()
{
EnsureSubsystemInstanceSet();
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()
{
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>();
}
}