Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 3082e56

Browse files
committed
Providing a quick VR/normal toggle key.
First notice that the keyboard event must be handled in the JS side since requesting the VR presentation is user-gated and only a synchronous call from a user gesture can start the presentationñ. The code in the C# behaviour compiles conditionally and it's only included if not targeting WebGL. This is because of Unity code for controlling the keyboard interferes somehow* with the code handling the input events in the JS. (*) When interfering, it resulted in the game not entering VR.
1 parent fe3b61e commit 3082e56

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

Assets/WebGLTemplates/WebVR/webvr.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
var sitStand = mat4.create();
2121
var gamepads = [];
2222
var vrGamepads = [];
23+
var toggleVRKeyName = '';
2324

2425
if ('serviceWorker' in navigator && 'isSecureContext' in window && !window.isSecureContext) {
2526
console.warn('The site is insecure; Service Workers will not work and the site will not be recognized as a PWA');
@@ -63,6 +64,11 @@
6364
vrDisplay.requestAnimationFrame(onAnimate);
6465
}
6566
}
67+
68+
// Handle quick VR/normal toggling.
69+
if (msg.detail.indexOf('ConfigureToggleVRKeyName') === 0) {
70+
toggleVRKeyName = msg.detail.split(':')[1];
71+
}
6672
}
6773

6874
function onToggleVR() {
@@ -317,13 +323,20 @@
317323
});
318324
}
319325

326+
function onKeyUp(evt) {
327+
if (toggleVRKeyName && toggleVRKeyName === evt.key) {
328+
onToggleVR();
329+
}
330+
}
331+
320332
// Monkeypatch `rAF` so that we can render at the VR display's framerate.
321333
window.requestAnimationFrame = onRequestAnimationFrame;
322334

323335
window.addEventListener('resize', onResize, true);
324336
window.addEventListener('vrdisplaypresentchange', onResize, false);
325337
window.addEventListener('vrdisplayactivate', onRequestPresent, false);
326338
window.addEventListener('vrdisplaydeactivate', onExitPresent, false);
339+
window.addEventListener('keyup', onKeyUp, false);
327340
document.addEventListener('UnityLoaded', onUnityLoaded, false);
328341
document.addEventListener('Unity', onUnity);
329342
entervrButton.addEventListener('click', onToggleVR, false);

Assets/WebVR/Plugins/WebGL/webvr.jslib

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ mergeInto(LibraryManager.library, {
1010

1111
PostRender: function () {
1212
document.dispatchEvent(new CustomEvent('Unity', {detail: 'PostRender'}));
13+
},
14+
15+
ConfigureToggleVRKeyName: function (keyName) {
16+
document.dispatchEvent(new CustomEvent('Unity', {detail: 'ConfigureToggleVRKeyName:'+Pointer_stringify(keyName)}));
1317
}
1418
});

Assets/WebVR/Prefabs/WebVRCameraSet.prefab

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ MonoBehaviour:
284284
m_Script: {fileID: 11500000, guid: 2330d5cea21bb564e97ad081f15487d9, type: 3}
285285
m_Name:
286286
m_EditorClassIdentifier:
287-
leftHandObj: {fileID: 0}
288-
rightHandObj: {fileID: 0}
287+
leftHandObject: {fileID: 0}
288+
rightHandObject: {fileID: 0}
289+
toggleVRKeyName: v
289290
--- !u!124 &124144808855090856
290291
Behaviour:
291292
m_ObjectHideFlags: 1

Assets/WebVR/Scripts/WebVRCamera.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class WebVRCamera : MonoBehaviour
1414
[DllImport("__Internal")]
1515
private static extern void PostRender();
1616

17+
[DllImport("__Internal")]
18+
private static extern void ConfigureToggleVRKeyName(string keyName);
19+
1720
Camera cameraMain, cameraL, cameraR;
1821

1922
Quaternion cq;
@@ -39,9 +42,14 @@ public class WebVRCamera : MonoBehaviour
3942

4043
bool active = false; // vr mode
4144

42-
public GameObject leftHandObj;
43-
public GameObject rightHandObj;
45+
[Tooltip("GameObject to be controlled by the left hand controller.")]
46+
public GameObject leftHandObject;
47+
48+
[Tooltip("GameObject to be controlled by the right hand controller.")]
49+
public GameObject rightHandObject;
4450

51+
[Tooltip("Name of the key used to alternate between VR and normal mode. Leave in blank to disable.")]
52+
public string toggleVRKeyName;
4553

4654
// delta time for latency checker.
4755
float deltaTime = 0.0f;
@@ -143,9 +151,12 @@ public void TogglePerf() {
143151
}
144152

145153
private void toggleMode() {
146-
active = active == true ? false : true;
147-
string mode = active == true ? "vr" : "normal";
154+
active = !active;
155+
string mode = active ? "vr" : "normal";
156+
157+
#if UNITY_EDITOR || !UNITY_WEBGL
148158
changeMode (mode);
159+
#endif
149160
}
150161

151162
private void changeMode(string mode)
@@ -192,20 +203,30 @@ void Start()
192203

193204
#if !UNITY_EDITOR && UNITY_WEBGL
194205
FinishLoading();
206+
ConfigureToggleVRKeyName(toggleVRKeyName);
195207
#endif
196208
}
197209

198210
void Update()
199211
{
212+
#if UNITY_EDITOR || !UNITY_WEBGL
213+
bool quickToggleEnabled = toggleVRKeyName != null && toggleVRKeyName != "";
214+
if (quickToggleEnabled) {
215+
if (Input.GetKeyUp(toggleVRKeyName)) {
216+
toggleMode();
217+
}
218+
}
219+
#endif
220+
200221
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
201222

202-
if (leftHandObj) {
203-
leftHandObj.transform.rotation = lhr;
204-
leftHandObj.transform.position = lhp + transform.position;
223+
if (leftHandObject) {
224+
leftHandObject.transform.rotation = lhr;
225+
leftHandObject.transform.position = lhp + transform.position;
205226
}
206-
if (rightHandObj) {
207-
rightHandObj.transform.rotation = rhr;
208-
rightHandObj.transform.position = rhp + transform.position;
227+
if (rightHandObject) {
228+
rightHandObject.transform.rotation = rhr;
229+
rightHandObject.transform.position = rhp + transform.position;
209230
}
210231

211232
if (active) {

0 commit comments

Comments
 (0)