Forum Discussion
viteichris
8 years agoProtege
How do I correctly implement VR focus with Unity (To pass VRC.PC.Input.1)
I'm trying to make sure our app is ready to go, but I'm not certain on what exactly I can/should do to ensure we pass this VRC: Currently we drop into our pause menu when the app loses focus, but it...
Anoki1972
5 years agoExplorer
Here a code that you attach to a game object in your scene and it works for me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GamePause : MonoBehaviour
{
public GameObject OVRCameraRig;
public GameObject pauseMenuUIThing;
public GameObject[] listOfObjectsToHideOnPause;
private AudioSource[] audioSources;
private void OnEnable()
{
// try to find camera rig automatically, if it's not set in the Inspector..
if (OVRCameraRig == null)
OVRCameraRig = GameObject.Find("OVRCameraRig");
// this subscribes to Oculus events, so when they fire our functions get called in this script
OVRManager.HMDUnmounted += PauseGame;
OVRManager.HMDMounted += UnPauseGame;
OVRManager.VrFocusLost += PauseGame;
OVRManager.VrFocusAcquired += UnPauseGame;
OVRManager.InputFocusLost += PauseGame;
OVRManager.InputFocusAcquired += UnPauseGame;
}
private void OnDisable()
{
// this unsubscribes from the Oculus events when they're no longer needed (when this object is disabled etc)
OVRManager.HMDUnmounted -= PauseGame;
OVRManager.HMDMounted -= UnPauseGame;
OVRManager.VrFocusLost -= PauseGame;
OVRManager.VrFocusAcquired -= UnPauseGame;
OVRManager.InputFocusLost -= PauseGame;
OVRManager.InputFocusAcquired -= UnPauseGame;
}
private void PauseGame()
{
//Debug.Log("PAUSE! --------------------------------------------");
// show menu message thing, if we have one..
if (pauseMenuUIThing != null)
pauseMenuUIThing.SetActive(true);
// if we have objects to hide, let's hide them..
for(int i=0; i< listOfObjectsToHideOnPause.Length;i++)
{
listOfObjectsToHideOnPause.SetActive(false);
}
// find and start all audiosources in the Scene
AudioSource[] audioSources = GameObject.FindObjectsOfTypeAll(typeof(AudioSource)) as AudioSource[];
for (int i = 0; i < audioSources.Length; i++)
{
audioSources.Pause();
}
// pause time
Time.timeScale = 0.0f;
}
private void UnPauseGame()
{
//Debug.Log("UNPAUSE! --------------------------------------------");
if (OVRManager.hasVrFocus && OVRManager.isHmdPresent && OVRManager.hasInputFocus)
{
// hide menu message thing, if we have one..
if (pauseMenuUIThing != null)
pauseMenuUIThing.SetActive(false);
// if we have objects to hide, let's hide them..
for (int i = 0; i < listOfObjectsToHideOnPause.Length; i++)
{
listOfObjectsToHideOnPause.SetActive(true);
}
// find and stop all audioSources in the Scene
AudioSource[] audioSources = GameObject.FindObjectsOfTypeAll(typeof(AudioSource)) as AudioSource[];
for (int i = 0; i < audioSources.Length; i++)
{
audioSources.UnPause();
}
// restart time
Time.timeScale = 1.0f;
}
}
}
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device