SteamVR_Behaviour.cs
7.58 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using XRSettings = UnityEngine.VR.VRSettings;
using XRDevice = UnityEngine.VR.VRDevice;
#endif
namespace Valve.VR
{
public class SteamVR_Behaviour : MonoBehaviour
{
private const string openVRDeviceName = "OpenVR";
public static bool forcingInitialization = false;
private static SteamVR_Behaviour _instance;
public static SteamVR_Behaviour instance
{
get
{
if (_instance == null)
{
Initialize(false);
}
return _instance;
}
}
public bool initializeSteamVROnAwake = true;
public bool doNotDestroy = true;
[HideInInspector]
public SteamVR_Render steamvr_render;
internal static bool isPlaying = false;
private static bool initializing = false;
public static void Initialize(bool forceUnityVRToOpenVR = false)
{
if (_instance == null && initializing == false)
{
initializing = true;
GameObject steamVRObject = null;
if (forceUnityVRToOpenVR)
forcingInitialization = true;
SteamVR_Render renderInstance = GameObject.FindObjectOfType<SteamVR_Render>();
if (renderInstance != null)
steamVRObject = renderInstance.gameObject;
SteamVR_Behaviour behaviourInstance = GameObject.FindObjectOfType<SteamVR_Behaviour>();
if (behaviourInstance != null)
steamVRObject = behaviourInstance.gameObject;
if (steamVRObject == null)
{
GameObject objectInstance = new GameObject("[SteamVR]");
_instance = objectInstance.AddComponent<SteamVR_Behaviour>();
_instance.steamvr_render = objectInstance.AddComponent<SteamVR_Render>();
}
else
{
behaviourInstance = steamVRObject.GetComponent<SteamVR_Behaviour>();
if (behaviourInstance == null)
behaviourInstance = steamVRObject.AddComponent<SteamVR_Behaviour>();
if (renderInstance != null)
behaviourInstance.steamvr_render = renderInstance;
else
{
behaviourInstance.steamvr_render = steamVRObject.GetComponent<SteamVR_Render>();
if (behaviourInstance.steamvr_render == null)
behaviourInstance.steamvr_render = steamVRObject.AddComponent<SteamVR_Render>();
}
_instance = behaviourInstance;
}
if (_instance != null && _instance.doNotDestroy)
GameObject.DontDestroyOnLoad(_instance.transform.root.gameObject);
initializing = false;
}
}
protected void Awake()
{
isPlaying = true;
if (initializeSteamVROnAwake && forcingInitialization == false)
InitializeSteamVR();
}
public void InitializeSteamVR(bool forceUnityVRToOpenVR = false)
{
if (forceUnityVRToOpenVR)
{
forcingInitialization = true;
if (initializeCoroutine != null)
StopCoroutine(initializeCoroutine);
if (XRSettings.loadedDeviceName == openVRDeviceName)
EnableOpenVR();
else
initializeCoroutine = StartCoroutine(DoInitializeSteamVR(forceUnityVRToOpenVR));
}
else
{
SteamVR.Initialize(false);
}
}
private Coroutine initializeCoroutine;
#if UNITY_2018_3_OR_NEWER
private bool loadedOpenVRDeviceSuccess = false;
private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
{
XRDevice.deviceLoaded += XRDevice_deviceLoaded;
XRSettings.LoadDeviceByName(openVRDeviceName);
while (loadedOpenVRDeviceSuccess == false)
{
yield return null;
}
XRDevice.deviceLoaded -= XRDevice_deviceLoaded;
EnableOpenVR();
}
private void XRDevice_deviceLoaded(string deviceName)
{
if (deviceName == openVRDeviceName)
{
loadedOpenVRDeviceSuccess = true;
}
else
{
Debug.LogError("<b>[SteamVR]</b> Tried to async load: " + openVRDeviceName + ". Loaded: " + deviceName, this);
loadedOpenVRDeviceSuccess = true; //try anyway
}
}
#else
private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
{
XRSettings.LoadDeviceByName(openVRDeviceName);
yield return null;
EnableOpenVR();
}
#endif
private void EnableOpenVR()
{
XRSettings.enabled = true;
SteamVR.Initialize(false);
initializeCoroutine = null;
forcingInitialization = false;
}
#if UNITY_EDITOR
//only stop playing if the unity editor is running
private void OnDestroy()
{
isPlaying = false;
}
#endif
#if UNITY_2017_1_OR_NEWER
protected void OnEnable()
{
Application.onBeforeRender += OnBeforeRender;
SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
}
protected void OnDisable()
{
Application.onBeforeRender -= OnBeforeRender;
SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
}
protected void OnBeforeRender()
{
PreCull();
}
#else
protected void OnEnable()
{
Camera.onPreCull += OnCameraPreCull;
SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
}
protected void OnDisable()
{
Camera.onPreCull -= OnCameraPreCull;
SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
}
protected void OnCameraPreCull(Camera cam)
{
if (!cam.stereoEnabled)
return;
PreCull();
}
#endif
protected static int lastFrameCount = -1;
protected void PreCull()
{
if (OpenVR.Input != null)
{
// Only update poses on the first camera per frame.
if (Time.frameCount != lastFrameCount)
{
lastFrameCount = Time.frameCount;
SteamVR_Input.OnPreCull();
}
}
}
protected void FixedUpdate()
{
if (OpenVR.Input != null)
{
SteamVR_Input.FixedUpdate();
}
}
protected void LateUpdate()
{
if (OpenVR.Input != null)
{
SteamVR_Input.LateUpdate();
}
}
protected void Update()
{
if (OpenVR.Input != null)
{
SteamVR_Input.Update();
}
}
protected void OnQuit(VREvent_t vrEvent)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
}