Integrating Unity 5.6.2 with ARKit to Swift project

281 Views Asked by At

I am able to integrate the Unity project into my Swift project successfully, but can't able to run the Unity window as I am getting the below error: enter image description here ***Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key currentUnityController.

I know I am making a mistake somewhere but this already have taken a lot of time. I would appreciate if someone who has tried his hand with this error can help. Below is the code for reference in my AppDelegate:

import UIKit

//@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var unityWindow: UIWindow?
    var currentUnityController: UnityAppController!
    var application: UIApplication?
    var isUnityRunning = false

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    self.application = application
    currentUnityController = UnityAppController()
    currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
    UnityGetMainWindow().alpha = 1.0
    // 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) {
    if 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
    }
}
}

Attached is the screenshot of the error:

1

There are 1 best solutions below

1
On

Finally solved! I somehow able to locate the unityAppController object in ObjC generated header's under derived sources. This wasn't working with the sample app I was creating, I feel the bridging header's are not attached properly.