Awais Hussain
Awais Hussain

Reputation: 2470

Embedded Unity Application not displaying in iOS app

I currently have an iOS app and want to embed a Unity application as a subview within my application. I'v been struggling with this over the last couple months on and off.

I was getting issues with building my application for some time, but now it is building and running properly (almost). The view I would like the Unity application to display within isn't displaying anything.

For reference I am using Unity 2019.1.12f1 and XCode 10.3

I am NOT using Vuforia, its just a very basic Unity app with a model and basic animation.

My AppDelegate file is:

 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {

 var window: UIWindow?

 var application: UIApplication?


 @objc var currentUnityController: UnityAppController!

 var isUnityRunning = false

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     self.application = application
     unity_init(CommandLine.argc, CommandLine.unsafeArgv)

     currentUnityController = UnityAppController()
     currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)

     // first call to startUnity will do some init stuff, so just call it here and directly stop it again
     startUnity()
     stopUnity()


     return true
 }

 func applicationWillResignActive(_ application: UIApplication) {f isUnityRunning {
         currentUnityController.applicationWillResignActive(application)
     }
 }

 func applicationDidEnterBackground(_ application: UIApplication) {
     if isUnityRunning {
         currentUnityController.applicationDidEnterBackground(application)
     }
 }

 func applicationWillEnterForeground(_ application: UIApplication) {
     if isUnityRunning {
         currentUnityController.applicationWillEnterForeground(application)
     }
 }

 func applicationDidBecomeActive(_ application: UIApplication) {

     if isUnityRunning {
         currentUnityController.applicationDidBecomeActive(application)
     }
 }

 func applicationWillTerminate(_ application: UIApplication) {
     if isUnityRunning {
         currentUnityController.applicationWillTerminate(application)
     }

 }


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

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

I am then using this snippet of code to display the code Unity in the subview

 let appDelegate = UIApplication.shared.delegate as! AppDelegate
 appDelegate.startUnity()
 self.unityView = UnityGetGLView()

I am absolutely baffled as to why this is not working. Any help would be much appreciated.

Hope this is enough information. Please let me know if there is anything else you need to know.

Upvotes: 0

Views: 1358

Answers (1)

aalmigthy
aalmigthy

Reputation: 1311

Your delegate looks all good, but your ViewController is missing some pieces. Try this code:

import UIKit

class UnityViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.startUnity()
            showUnitySubView()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.stopUnity()
        }
    }

    func showUnitySubView() {
        if let unityView = UnityGetGLView() {
            // insert subview at index 0 ensures unity view is behind current UI view
            view?.insertSubview(unityView, at: 0)

            unityView.translatesAutoresizingMaskIntoConstraints = false
            let views = ["view": unityView]
            let w = NSLayoutConstraint.constraints(withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: views)
            let h = NSLayoutConstraint.constraints(withVisualFormat: "V:|-64-[view]-49-|", options: [], metrics: nil, views: views)
            view.addConstraints(w + h)
        }
    }
}

Upvotes: 1

Related Questions