我能夠將Unity項目成功整合到Swift項目中,但無法運行Unity窗口,因爲我收到以下錯誤: ** *因未捕獲異常'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
}
}
}
附件是錯誤的截圖:
我知道我添加的下面這一行無法獲得鍵「currentUnityController」的值。但如何解決這個問題? // ******** ADDED ******** NS_INLINE UnityAppController * GetAppController() { NSObject * delegate = [UIApplication sharedApplication] .delegate; UnityAppController * currentUnityController =(UnityAppController *)[delegate valueForKey:@「currentUnityController」]; return currentUnityController; } –
Tarun
還沒有幫助? :P – Tarun