How to quit / terminate entire unity game from native iOS

Viewed 2867

I followed below tutorials to integrate unity with iOS application
1. https://github.com/blitzagency/ios-unity5
2. https://www.agnosticdev.com/blog-entry/swift/integrating-unity-and-vuforia-ios-swift-project?page=1

I try to stop unity by following code from iOS

func stopUnity() {
        if isUnityRunning {
            isUnityRunning = false
            currentUnityController!.applicationWillResignActive(application!)
        }
    }

but when i try to start unity again by following code

func startUnity() {
        if !isUnityRunning {
            isUnityRunning = true
            currentUnityController!.applicationDidBecomeActive(application!)               
        }
    }

It only resume the unity game. I'd to restart the unity game instead of resuming.

2 Answers

Here is the thread with the same question.

I wish there are next ways to terminate unity:

1) Application.Quit - it calls Exit(0) so it terminates native app too. But we need to close only Unity and let the app work.

Quits the player application. Shut the running application down. Quit is ignored in the editor.

2) Application.Unload - this is what we want, depending on description, but i didn't get how to make with it yet.

Unload the Unity runtime. Similar to quitting but the application process does not exit. This is useful when using Unity as a component in another application. For example, if you use Unity to display some special content but do not want to keep the Unity engine resident in memory when it is not needed.

3) Call unityController?.applicationWillTerminate(UIApplication.shared) - didn't work for me. Crashed.
4) Add UIApplicationExitsOnSuspend - didn't try.

There i wrote my ideas about that problem. I know that it's not the real answer, but it's better than nothing and maybe broad somebody to the real nice-work solution :)

When using the "Unity as a Library" Feature to integrate Unity into your native iOS application you won't be able to fully terminate Unity and return to your native App.

As stated in the Unity Documentation on integrating Unity into native iOS applications you are able to call the method - (void)unloadApplication; on the UnityFramework instance to release most of the memory it occupies, but not all of it. You will be able to run Unity again after doing so.

var isInitialized: Bool {
    ufw?.appController() != nil
}

func unloadUnity() {
    if isInitialized {
        ufw?.unloadApplication()
    }
}
Related