我能够将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