Scene disappears on home button press, SwiftUI persists. How to get scene back?

I’ve created an Unbounded app that opens a SwiftUI window from my Unity scene. When I press the home button in the simulator (or the digital crown on the device), the scene disappears but the SwiftUI remains open. If I press home again, the SwiftUI window fades and the VisionOS menu appears. If I go back to my app, SwiftUI is coming to the foreground, but my scene is not appearing again. This is the log I get in Xcode, when i press the home button the first time and the Unity scene disappears:

Scene will resign active: <UIWindowScene: 0x103a1aed0; role: UISceneSessionRoleImmersiveSpaceApplication; persistentIdentifier: *com.app.id*:SFBSystemService-***95C; activationState: UISceneActivationStateForegroundActive>
Scene didEnterBackground: <UIWindowScene: 0x103a1aed0; role: UISceneSessionRoleImmersiveSpaceApplication; persistentIdentifier: *com.app.id*:SFBSystemService-***95C; activationState: UISceneActivationStateBackground>
Window will enter background: uuid ***348
Scene disconnected: <UIWindowScene: 0x103a1aed0; role: UISceneSessionRoleImmersiveSpaceApplication; persistentIdentifier: *com.app.id*:SFBSystemService-***95C; activationState: UISceneActivationStateUnattached>
Window removed: uuid ***348
   ... window uuid ***348 was assigned to 8712
Volume 8712: window closed, uuid: ***348

No OnDisable is called from my Unity scene, and logging continues from my scripts in the scene after the scene has disappeared in the simulator.
Clicking the buttons in SwiftUI still makes the scripts attached to the scene react, so the scene is still hooked up in some way. I just have no idea how to bring it back visually.

How do I enable the scene again when coming back to my app?

Using PolySpatial 1.1.6, Xcode 15.3, Simulator 15.3 for Apple Vision Pro (1.1). Same issue on Vision Pro with 1.1.2).

1 Like

I had the same issue and started to close the native UI when the app was in the background and reopen it when I came back to it.

Since OnApplicationFocus and OnApplicationPause are not called on visionOS device, I used “On Window Opened” and “On Window Closed” events for the same purpose.

Here someone pointed these events out:

1 Like

@berkanozgur
Can you send the code for me to see? I tried it myself but it didn’t work

Assuming that you implemented your Swift UI plugin correctly, meaning that your open and close functions work.
First you need to add a public method into your SwiftUIDriver.cs (Script that have DLLImport from your plugin, you add this script to your scene) with WindowState parameter and then add the method to the Events section of your VolumeCamera.
That part of the script will look like this:

    public void WindowEvent(WindowState windowState) //Set this public method into your VolumeCamera>Events in the inspector, dont forget to Choose Dynamic WindowState!
    {
        switch (windowState.WindowEvent)
        {
            case VolumeCamera.WindowEvent.Opened: //Open window when your app is on focus
                GoForeground();
                break;
            case VolumeCamera.WindowEvent.Backgrounded: //Close window when your app is on background
                GoBackground();
                break;
            default:
                break;
        }
    }
    void GoForeground()
    {
        OpenSwiftUIWindow("PanelName"); //Imported native function located in your version of SwittUIPlugin.swift
        nativeWindowOpen = true;
    }
    void GoBackground()
    {
        CloseSwiftUIWindow("PanelName"); //Imported native function located in your version of SwittUIPlugin.swift
        nativeWindowOpen = false;
    }

Dont forget that if you directly use this code it assumes your native ui window is always open, you need to update it for your use

1 Like