Skip to content

Commit 7724993

Browse files
committed
use SubsystemWithProvider for newer version
1 parent 7c03a8d commit 7724993

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

Packages/webxr/Runtime/Scripts/WebXRManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ public enum WebXRVisibilityState
1111
}
1212

1313
[DefaultExecutionOrder(-2020)]
14+
#if UNITY_2022_2_OR_NEWER || UNITY_2023_1_OR_NEWER
15+
public class WebXRManager : SubsystemLifecycleManager<WebXRSubsystem, WebXRSubsystemDescriptor, WebXRSubsystemProvider>
16+
#else
1417
public class WebXRManager : SubsystemLifecycleManager<WebXRSubsystem, WebXRSubsystemDescriptor>
18+
#endif
1519
{
1620
public static WebXRManager Instance { get; private set; }
1721

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
1-
using System;
1+
using System;
22
using System.Runtime.InteropServices;
33
using AOT;
44
using UnityEngine;
55

6+
#if UNITY_2022_2_OR_NEWER || UNITY_2023_1_OR_NEWER
7+
using UnityEngine.SubsystemsImplementation;
8+
#endif
9+
610
namespace WebXR
711
{
8-
// TODO: we need an XRInputSubsystem implementation - this can only be done via native code
12+
#if UNITY_2022_2_OR_NEWER || UNITY_2023_1_OR_NEWER
13+
public class WebXRSubsystemDescriptor : SubsystemDescriptorWithProvider<WebXRSubsystem,WebXRSubsystemProvider>
14+
{
15+
public WebXRSubsystemDescriptor()
16+
{
17+
providerType = typeof(WebXRSubsystem.Provider);
18+
}
19+
}
20+
21+
public abstract class WebXRSubsystemProvider : SubsystemProvider<WebXRSubsystem> { }
22+
23+
public class WebXRSubsystem : SubsystemWithProvider<WebXRSubsystem,WebXRSubsystemDescriptor,WebXRSubsystemProvider>
24+
{
25+
public class Provider : WebXRSubsystemProvider
26+
{
27+
public override void Start() { }
28+
public override void Stop() { }
29+
public override void Destroy() { }
30+
}
31+
32+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
33+
private static void RegisterDescriptor()
34+
{
35+
SubsystemDescriptorStore.RegisterDescriptor(new WebXRSubsystemDescriptor() {
36+
id = typeof(WebXRSubsystem).FullName
37+
});
38+
}
39+
40+
internal static WebXRSubsystem Instance;
41+
protected override void OnStart()
42+
{
43+
if (Instance != null) return;
44+
Debug.Log("Start " + nameof(WebXRSubsystem));
45+
Instance = this;
46+
InternalStart();
47+
}
948

49+
protected override void OnStop()
50+
{
51+
if (Instance == null) return;
52+
Debug.Log("Stop " + nameof(WebXRSubsystem));
53+
Instance = null;
54+
}
55+
56+
protected override void OnDestroy()
57+
{
58+
if (Instance == null) return;
59+
Debug.Log("Destroy " + nameof(WebXRSubsystem));
60+
Instance = null;
61+
}
62+
#else
1063
public class WebXRSubsystemDescriptor : SubsystemDescriptor<WebXRSubsystem>
1164
{
1265
}
@@ -51,6 +104,11 @@ protected override void OnDestroy()
51104
Instance = null;
52105
}
53106

107+
private bool _running;
108+
public override bool running => _running;
109+
110+
private static WebXRSubsystem Instance;
111+
#endif
54112
private void UpdateControllersOnEnd()
55113
{
56114
if (OnHandUpdate != null)
@@ -161,12 +219,7 @@ private void UpdateXRCameras()
161219
}
162220
}
163221

164-
private bool _running;
165-
public override bool running => _running;
166-
167-
private static WebXRSubsystem Instance;
168-
169-
private void InternalStart()
222+
internal void InternalStart()
170223
{
171224
#if UNITY_WEBGL
172225
Native.SetWebXREvents(OnStartAR, OnStartVR, UpdateVisibilityState, OnEndXR, OnXRCapabilities, OnInputProfiles);
@@ -570,4 +623,4 @@ bool GetHitTestPoseFromViewerHitTestPoseArray(ref WebXRHitPoseData hitPoseData)
570623
return true;
571624
}
572625
}
573-
}
626+
}

Packages/webxr/Runtime/XRPlugin/XRSystemLifecycleManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22
using UnityEngine;
33
using UnityEngine.XR.Management;
44

5+
#if UNITY_2022_2_OR_NEWER || UNITY_2023_1_OR_NEWER
6+
using UnityEngine.SubsystemsImplementation;
7+
#endif
8+
59
namespace WebXR
610
{
11+
#if UNITY_2022_2_OR_NEWER || UNITY_2023_1_OR_NEWER
12+
public class SubsystemLifecycleManager<TSubsystem, TSubsystemDescriptor,TProvider> : MonoBehaviour
13+
where TSubsystem : SubsystemWithProvider<TSubsystem, TSubsystemDescriptor,TProvider>, new()
14+
where TSubsystemDescriptor : SubsystemDescriptorWithProvider
15+
where TProvider : SubsystemProvider<TSubsystem>
16+
#else
717
public class SubsystemLifecycleManager<TSubsystem, TSubsystemDescriptor> : MonoBehaviour
818
where TSubsystem : Subsystem<TSubsystemDescriptor>
919
where TSubsystemDescriptor : SubsystemDescriptor<TSubsystem>
20+
#endif
1021
{
1122
/// <summary>
1223
/// Get the <c>TSubsystem</c> whose lifetime this component manages.

0 commit comments

Comments
 (0)