2017-09-01 64 views
1

我能够将Unity项目成功整合到Swift项目中,但无法运行Unity窗口,因为我收到以下错误: enter image description here ** *因未捕获异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类不是关键字currentUnityController的关键字值编码。将Unity 5.6.2与ARKit集成到Swift项目中

我知道我在某个地方犯了一个错误,但这已经花费了很多时间。如果有人尝试过这个错误,我会很感激。下面是代码以供参考我的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 
    } 
} 
} 

附件是错误的截图:

+0

我知道我添加的下面这一行无法获得键“currentUnityController”的值。但如何解决这个问题? // ******** ADDED ******** NS_INLINE UnityAppController * GetAppController() { NSObject * delegate = [UIApplication sharedApplication] .delegate; UnityAppController * currentUnityController =(UnityAppController *)[delegate valueForKey:@“currentUnityController”]; return currentUnityController; } – Tarun

+0

还没有帮助? :P – Tarun

回答

0

终于解决了!我以某种方式能够找到ObjC生成的标题下的派生源的unityAppController对象。这不适用于我创建的示例应用程序,我觉得桥接标头未正确连接。

+0

我可以通过转到目标的“Build Settings”并将“Swift Language Version”从4.0设置为3.2来修复它。谢谢埃里克! – Tarun