XRGeneralSettings.cs
6.69 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.XR.Management
{
/// <summary>General settings container used to house the instance of the active settings as well as the manager
/// instance used to load the loaders with.
/// </summary>
public class XRGeneralSettings : ScriptableObject
{
/// <summary>The key used to query to get the current loader settings.</summary>
public static string k_SettingsKey = "com.unity.xr.management.loader_settings";
internal static XRGeneralSettings s_RuntimeSettingsInstance = null;
[SerializeField]
internal XRManagerSettings m_LoaderManagerInstance = null;
[SerializeField]
internal bool m_InitManagerOnStart = true;
/// <summary>The current active manager used to manage XR lifetime.</summary>
public XRManagerSettings Manager
{
get { return m_LoaderManagerInstance; }
set { m_LoaderManagerInstance = value; }
}
private XRManagerSettings m_XRManager = null;
#pragma warning disable 414 // Suppress warning for needed variables.
private bool m_ProviderIntialized = false;
private bool m_ProviderStarted = false;
#pragma warning restore 414
/// <summary>The current settings instance.</summary>
public static XRGeneralSettings Instance
{
get
{
return s_RuntimeSettingsInstance;
}
#if UNITY_EDITOR
set
{
s_RuntimeSettingsInstance = value;
}
#endif
}
/// <summary>The current active manager used to manage XR lifetime.</summary>
public XRManagerSettings AssignedSettings
{
get
{
return m_LoaderManagerInstance;
}
#if UNITY_EDITOR
set
{
m_LoaderManagerInstance = value;
}
#endif
}
/// <summary>Used to set if the manager is activated and initialized on startup.</summary>
public bool InitManagerOnStart
{
get
{
return m_InitManagerOnStart;
}
#if UNITY_EDITOR
set
{
m_InitManagerOnStart = value;
}
#endif
}
#if !UNITY_EDITOR
void Awake()
{
Debug.Log("XRGeneral Settings awakening...");
s_RuntimeSettingsInstance = this;
Application.quitting += Quit;
DontDestroyOnLoad(s_RuntimeSettingsInstance);
}
#endif
#if UNITY_EDITOR
void Pause()
{
if (m_ProviderIntialized && m_ProviderStarted)
{
StopXRSDK();
}
}
void Unpause()
{
if (m_ProviderIntialized && !m_ProviderStarted)
{
StartXRSDK();
}
}
public void InternalPauseStateChanged(PauseState state)
{
switch (state)
{
case PauseState.Paused:
Pause();
break;
case PauseState.Unpaused:
Unpause();
break;
}
}
/// <summary>For internal use only.</summary>
public void InternalPlayModeStateChanged(PlayModeStateChange state)
{
switch (state)
{
case PlayModeStateChange.ExitingPlayMode:
Quit();
break;
case PlayModeStateChange.ExitingEditMode:
case PlayModeStateChange.EnteredPlayMode:
case PlayModeStateChange.EnteredEditMode:
break;
}
}
#endif
static void Quit()
{
XRGeneralSettings instance = XRGeneralSettings.Instance;
if (instance == null)
return;
instance.OnDisable();
instance.OnDestroy();
}
void Start()
{
StartXRSDK();
}
void OnDisable()
{
StopXRSDK();
}
void OnDestroy()
{
DeInitXRSDK();
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
internal static void AttemptInitializeXRSDKOnLoad()
{
XRGeneralSettings instance = XRGeneralSettings.Instance;
if (instance == null || !instance.InitManagerOnStart)
return;
instance.InitXRSDK();
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
internal static void AttemptStartXRSDKOnBeforeSplashScreen()
{
XRGeneralSettings instance = XRGeneralSettings.Instance;
if (instance == null || !instance.InitManagerOnStart)
return;
instance.StartXRSDK();
}
private void InitXRSDK()
{
if (XRGeneralSettings.Instance == null || XRGeneralSettings.Instance.m_LoaderManagerInstance == null || XRGeneralSettings.Instance.m_InitManagerOnStart == false)
return;
m_XRManager = XRGeneralSettings.Instance.m_LoaderManagerInstance;
if (m_XRManager == null)
{
Debug.LogError("Assigned GameObject for XR Management loading is invalid. No XR Providers will be automatically loaded.");
return;
}
m_XRManager.automaticLoading = false;
m_XRManager.automaticRunning = false;
m_XRManager.InitializeLoaderSync();
m_ProviderIntialized = true;
}
private void StartXRSDK()
{
if (m_XRManager != null && m_XRManager.activeLoader != null)
{
m_XRManager.StartSubsystems();
m_ProviderStarted = true;
}
}
private void StopXRSDK()
{
if (m_XRManager != null && m_XRManager.activeLoader != null)
{
m_XRManager.StopSubsystems();
m_ProviderStarted = false;
}
}
private void DeInitXRSDK()
{
if (m_XRManager != null && m_XRManager.activeLoader != null)
{
m_XRManager.DeinitializeLoader();
m_XRManager = null;
m_ProviderIntialized = false;
}
}
}
}