Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
OzzyMonstar's avatar
OzzyMonstar
Honored Guest
7 years ago

pause when dash opens

Hello i built my game in unity and everything works but now when i add dash support the game won't pause. I read something about setting oculus focus aware to true but how would i do that or where would i do that if that is the solution? I just need the game to pause when i launch Dash.

6 Replies

Replies have been turned off for this discussion


  • Hi,

    Welcome to the Oculus Developer Forums.

    Please read the documentation on incorporating dash support into your unity project here:

    https://developer.oculus.com/documentation/unity/latest/concepts/unity-dash/

    From the documentation:

    To check if your app has focus input, query OVRManager.hasInputFocus every frame. If your app has focus hasInputFocus will return true. If the user's focus is elsewhere, like when the user opens the Dash menu or removes their HMD, hasInputFocus will return false.

    In
    single-player apps or experiences, you can pause the app, mute audio
    playback, and stop rendering any tracked controllers/hands present in
    the scene (Dash will use a separate set of hands).

    Multiplayer
    experiences may wish to handle the loss of input focus differently.
    You're required to hide the hands and ignore any input while the app
    does not have focus input, but you may wish to continue audio playback
    and the match in the background.

    For more information on OVRManager.hasInputFocus, see Application Lifecycle Handling.





    Thanks for your response, when i open ovrmanager here is some of the code i found with .hasvrfocus, is this correct or should i add or change something?

    public static string audioInId
     {
      get { return OVRPlugin.audioInId; }
     }
     private static bool _hasVrFocusCached = false;
     private static bool _hasVrFocus = false;
     private static bool _hadVrFocus = false;
     /// <summary>
     /// If true, the app has VR Focus.
     /// </summary>
     public static bool hasVrFocus
     {
      get {
       if (!_hasVrFocusCached)
       {
        _hasVrFocusCached = true;
        _hasVrFocus = OVRPlugin.hasVrFocus;
       }
       return _hasVrFocus;
      }
      private set {
       _hasVrFocusCached = true;
       _hasVrFocus = value;
      }
     }
     private static bool _hadInputFocus = true;
     /// <summary>
     /// If true, the app has Input Focus.
     /// </summary>
     public static bool hasInputFocus
     {
      get
      {
       return OVRPlugin.hasInputFocus;
      }
     }
     /// <summary>
     /// If true, then the Oculus health and safety warning (HSW) is currently visible.
     /// </summary>
     [Obsolete]
     public static bool isHSWDisplayed { get { return false; } }


  • Hi,

    Welcome to the Oculus Developer Forums.

    Please read the documentation on incorporating dash support into your unity project here:

    https://developer.oculus.com/documentation/unity/latest/concepts/unity-dash/

    From the documentation:

    To check if your app has focus input, query OVRManager.hasInputFocus every frame. If your app has focus hasInputFocus will return true. If the user's focus is elsewhere, like when the user opens the Dash menu or removes their HMD, hasInputFocus will return false.

    In
    single-player apps or experiences, you can pause the app, mute audio
    playback, and stop rendering any tracked controllers/hands present in
    the scene (Dash will use a separate set of hands).

    Multiplayer
    experiences may wish to handle the loss of input focus differently.
    You're required to hide the hands and ignore any input while the app
    does not have focus input, but you may wish to continue audio playback
    and the match in the background.

    For more information on OVRManager.hasInputFocus, see Application Lifecycle Handling.





    Thanks for your response, when i open ovrmanager here is some of the code i found with .hasvrfocus, is this correct or should i add or change something?

    public static string audioInId
     {
      get { return OVRPlugin.audioInId; }
     }
     private static bool _hasVrFocusCached = false;
     private static bool _hasVrFocus = false;
     private static bool _hadVrFocus = false;
     /// <summary>
     /// If true, the app has VR Focus.
     /// </summary>
     public static bool hasVrFocus
     {
      get {
       if (!_hasVrFocusCached)
       {
        _hasVrFocusCached = true;
        _hasVrFocus = OVRPlugin.hasVrFocus;
       }
       return _hasVrFocus;
      }
      private set {
       _hasVrFocusCached = true;
       _hasVrFocus = value;
      }
     }
     private static bool _hadInputFocus = true;
     /// <summary>
     /// If true, the app has Input Focus.
     /// </summary>
     public static bool hasInputFocus
     {
      get
      {
       return OVRPlugin.hasInputFocus;
      }
     }
     /// <summary>
     /// If true, then the Oculus health and safety warning (HSW) is currently visible.
     /// </summary>
     [Obsolete]
     public static bool isHSWDisplayed { get { return false; } }
  • Anonymous's avatar
    Anonymous

    Dear greetings, I am stuck in the same problem, I can not
    get the game paused when the user removes the HMD. Could someone solve it, that
    code work?


  • Here is my sample code

    This is a base class. This class cannot attach to any object, because it is abstract class. (Please read till the end)

    using UnityEngine;
    public abstract class BaseOVRFocusHandler : MonoBehaviour
    {
        private bool isPaused = false;
        private void OnEnable()
        {
            OVRManager.HMDUnmounted += PauseGame;
            OVRManager.HMDMounted += UnPauseGame;
            OVRManager.VrFocusLost += PauseGame;
            OVRManager.VrFocusAcquired += UnPauseGame;
            OVRManager.InputFocusLost += PauseGame;
            OVRManager.InputFocusAcquired += UnPauseGame;
        }

        private void OnDisable()
        {
            OVRManager.HMDUnmounted -= PauseGame;
            OVRManager.HMDMounted -= UnPauseGame;
            OVRManager.VrFocusLost -= PauseGame;
            OVRManager.VrFocusAcquired -= UnPauseGame;
            OVRManager.InputFocusLost -= PauseGame;
            OVRManager.InputFocusAcquired -= UnPauseGame;
        }

        private void PauseGame()
        {
            if ( !isPaused )
            {
                DisableAudioListener(true);
                Global.isGameFocusGone = true;
                Time.timeScale = 0.0f;
                Time.fixedDeltaTime = 0;
                OnGamePause();
            }
        }

        private void UnPauseGame()
        {
            if (OVRManager.hasVrFocus && OVRManager.isHmdPresent && OVRManager.hasInputFocus)
            {
                Time.timeScale = 1.0f;
                Time.fixedDeltaTime = 1f / 90f;
                Global.isGameFocusGone = false;
                DisableAudioListener(false);
                OnGameUnPause();
                isPaused = false;
            }
        }


        protected abstract void OnGamePause();

        protected abstract void OnGameUnPause();

        private void DisableAudioListener(bool canEnable)
        {
            AudioListener.pause = canEnable;
        }
    }



    Here is a derived class, this can attach to the object. If you have only one scene, you can use the same script, otherwise, you have to modify the code as a Singleton or DontDestroyOnLoad

    public class AutoGamePauseManager : BaseOVRFocusHandler
    {
        protected override void OnGamePause()
        {
            // try your code
        }

        protected override void OnGameUnPause()
        {
            // try your code
        }
    }





  • 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;
    }
    }

    }