Wednesday, November 11, 2026
HomeiOS Developmentios - SwiftUI and Unity integration window hierarchy

ios – SwiftUI and Unity integration window hierarchy

[ad_1]

I am making an attempt to make an Unity participant integration inside a local SwiftUI app. The participant ought to seem when taping a button then person can shut the participant from Unity and get again to the SwiftUI app.

For the second I can handle to show the Unity participant with this code

SwiftUI View from the place I wish to show the participant

//Some SwiftUI View
NavigationView {
 SomeView()
   .fullScreenCover(isPresented: $applicationState.unityPlayerActive, onDismiss: {}, content material: {
     //This can be a UIViewControllerRepresentable that decision the unity.present() operate from Unity Framework
      UnityPlayer()
        .ignoresSafeArea()
   })
}

UnityPlayer.swift

struct UnityPlayer: UIViewControllerRepresentable {
    func makeUIViewController(context _: Context) -> UIViewController {
        let vc = UIViewController()
        let unity = UnityBridge.getInstance()
        
        
        DispatchQueue.major.asyncAfter(deadline: .now() + 0.5) {
            unity.present()
        }
        return vc
    }

    func updateUIViewController(_: UIViewController, context _: Context) {
      // Empty.
    }
}

UnityBridge.swift

...
    /// Notifies the UnityFramework to indicate the window, and append the Unity view
    /// to the given controller
    ///
    /// - Parameter controller: Controller that can host the Unity view
    public func present() {
        ufw.showUnityWindow()
    }

    /// Triggered by Unity by way of `UnityFrameworkListener` when the framework unloaded
    inner func unityDidUnload(_: Notification!) {
        ufw.unregisterFrameworkListener(self)
        UnityBridge.occasion = nil
        
        self.view?.removeFromSuperview()
        
        NotificationCenter.default.put up(identify: Notification.Title(closeUnityPlayerNotification), object: nil)
    }
...

This code works however when the participant is closed the app turn into unresponsive, the participant continues to be in foreground capturing person’s inputs.

I haven’t got any error message.

Thanks in your assist 🙂

For reference I’ve adopted the work of David Peicho that designate effectively the essential logic

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments