2

I'm working on an Oculus Go app in Unity and found that quitting the app in the headset does not call onApplicationQuit(). Does anyone know a good workaround for this? I really need to have some code that only executes when the app closes.

I've tried using OnDestroy() as a workaround, but it also is not called when the app is closed. OnPaused() is called when you enter the Oculus dialog that asks if you want to exit, not when you actually select exit.

JJIqbal
  • 630
  • 1
  • 8
  • 23
Zert
  • 31
  • 4
  • As a workaround you could always quit the app from within Unity using `Application.Quit();` .. than you could implement you logic right before it (or I guess also `onApplicatiomQuit` will than be called. – derHugo Apr 09 '19 at 04:07
  • I don't know how the Oculus works but maybe it is the same as for `iOS` and you have to check `Exit on suspend` ([OnApplicationQuit](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html))? – derHugo Apr 09 '19 at 04:12
  • Note that even the [SteamVR_Plugin](https://github.com/ValveSoftware/steamvr_unity_plugin/blob/master/Assets/SteamVR/Scripts/SteamVR_Render.cs) itself uses `OnApplicationQuit` so it seems strange that it isn't called – derHugo Apr 09 '19 at 04:23
  • 1
    I'm actually not using SteamVR, just the Oculus Utilities for Unity. Exit on Suspend seems to only be an option in iOS. I could provide a different way for the user to exit the app, but as far as I know there's no way to disable the home button that takes you to the Exit dialog, so users could still use it. This would cause an issue because the app downloads large files from online, and what I need to do on Exit is end any downloads that are in progress and delete the files. If the app exists without doing that, you get stuck with half-downloaded, unplayable video files. – Zert Apr 09 '19 at 20:48
  • @derHugo Oculus runs on Android, so `Exit on suspend` is not an option. :( – Yes Barry Sep 12 '19 at 22:13
  • @Zert you could go the other way round: Have some File with e.g. the names of successful completely downloaded videos. On app start check if all videos appear in this list, if not delete it and start a new download for this video file. Or also depends on the download.. you could wait until downloads complete before starting to write the files to storage maybe? – derHugo Sep 13 '19 at 06:45

2 Answers2

0

As you already stated, OnApplicationQuit() is never actually invoked when exiting an app on the Go from the Oculus menu. The app is instead placed into a suspended state for which little can be done. Aside from hooking into native code or some android java plugin (the details thereof are not my forte), there isn't much of a known workaround for this.

Part of this reason is because the workaround really does depend on the needs of the app, saving state or PlayerPrefs can be done in the OnApplicationPause() method.

private void OnApplicationPause(bool isPaused)
{
    if (isPaused) {
        // ..
        PlayerPrefs.Save();
    }    
}

This is probably the simplest solution, for single player/user games/apps. For multiplayer games, you can subscribe to OVR's connection state changed event and handle the user who just quit from the inversed side:

using Oculus.Platform;
using Oculus.Platform.Models;
// ...

Net.SetConnectionStateChangedCallback((Message<NetworkingPeer> message) => {
    if (message.Data.State == PeerConnectionState.Closed) {
        Debug.Log("The user MOST LIKELY quit the app.");
    }
});

This is not a perfect solution but since OVR doesn't provide anything in their docs and they don't respond to questions on this topic in their forums, this is a decent enough workaround.

The only other option is to encourage users to click on some sort of "Quit" button from within your app which calls UnityEngine.Application.Quit() which will indeed invoke the OnApplicationQuit() method.

Cheers.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
-1

If you are developing and publishing for the Oculus GO then you will need to use their Unity SDK which in return provides a function to quit the app properly.

I am guessing you cannot use OnApplicationQuit because in the Oculus Go requirement, the back button must only be used to return the user to the Oculus Go main menu.

First, you need to make sure you have the OVRmanager in your project which you get by downloading and importing the Unity SDK from the Oculus Developer website.

Then use the script below to quit the application and it will send you back to the Oculus Go Main menu when you press the back button on the controller.

if (OVRInput.GetDown(OVRInput.Button.Back)
{
  OVRManager.PlatformUIConfirmQuit();
}
STerrier
  • 3,755
  • 1
  • 16
  • 41
  • Pressing the main oculus button already does this by default. The problem is quitting this way never calls the `OnApplicationQuit()` method in the app. – Yes Barry Sep 12 '19 at 21:41
  • This was based on the Unity Tutorial and how they recommend it to quit. Works fine on my app which is published in Oculus store. Sorry I couldn't help you further – STerrier Sep 12 '19 at 23:23