2017-09-01 66 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