将Unity + Vuforia集成到现有iOS项目使用户交互无法正常工作

时间:2018-05-18 04:51:47

标签: swift unity3d vuforia

目前我正在关注this tutorial将Unity + Vuforia项目集成到我现有的iOS项目中。我设法能够在我的ARViewController中显示Unity视图。问题是我在视图控制器中丢失了所有用户交互:后退按钮的触摸事件不会启动。

import Foundation

class ARViewController: UIViewController {

    var unityView: UIView?

    static func instantiateViewController() -> ARViewController {
        let controller = UIStoryboard.main.instantiateViewController(withIdentifier: "ARViewController") as! ARViewController
        return controller
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.currentUnityController = UnityAppController()
            appDelegate.currentUnityController?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)
            appDelegate.startUnity()
            NotificationCenter.default.addObserver(self, selector: #selector(handleUnityReady), name: NSNotification.Name("UnityReady"), object: nil)
        }

    }

    @IBAction func onBackPressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.stopUnity()
        }
    }

    @objc func backPressedTest() {
    }

    @objc func handleUnityReady() {
        showUnitySubView()
    }

    func showUnitySubView() {

        guard let unityView = UnityGetGLView() else { return }

        self.unityView = unityView
        // insert subview at index 0 ensures unity view is behind current UI view
        view?.addSubview(unityView)

        unityView.translatesAutoresizingMaskIntoConstraints = false
        let views = ["view": unityView]
        let w = NSLayoutConstraint.constraints(withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: views)
        let h = NSLayoutConstraint.constraints(withVisualFormat: "V:|-50-[view]-0-|", options: [], metrics: nil, views: views)
        view.addConstraints(w + h)

        let button = UIButton(type: .custom)
        button.setImage(#imageLiteral(resourceName: "ic_back_black").withRenderingMode(.alwaysTemplate), for: .normal)
        button.addTarget(self, action:#selector(backPressed), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 28, height: 60)
        button.widthAnchor.constraint(equalToConstant: button.frame.width).isActive = true
        button.tintColor = UIColor.purpleBrown()
        view?.addSubview(button)

    }
}

我也注意到Unity的按钮在触摸时也有任何效果。绿色栏内的后退按钮来自Unity。蓝色按钮来自我的ARViewController。两者似乎都达不到触摸事件。 enter image description here

调试元素: enter image description here

2 个答案:

答案 0 :(得分:3)

当我将Unity配置放在application:didFinishLaunchingWithOptions:的顶部时,就在我现有配置之上,我在项目中使用的其他服务时,会发生这种情况。对于将来遇到此问题的人,请访问我的appDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.application = application

    // Another existing settings for the project, e.g. fabric

    Fabric.with([Crashlytics.self])

    // Put the Unity configuration at the bottom of the function

    unity_init(CommandLine.argc, CommandLine.unsafeArgv)
    currentUnityController = UnityAppController()
    currentUnityController?.application(application, didFinishLaunchingWithOptions: launchOptions)
    startUnity()
    stopUnity()

    return true
}

对于视图控制器中的viewWillAppear(_:)

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    setupBackBarButtonItems(back: true, isDark: true)
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.startUnity()
        showUnitySubView()
    }
}

正如@Prashant在评论中所提到的,UnityReady通知只会被调用一次。所以我不能使用它。

然后我只需拨打stopUnity()中的viewWillDisappear(_:)

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.stopUnity()
    }
}

目前的问题是,如果我离开屏幕,我就无法杀死统一进程。它已知bug,如果可能的话,我还在弄清楚如何做到这一点。

答案 1 :(得分:2)

我遇到了同样的问题,但是将Unity配置移到applicationDidFinishLaunchingWithOption方法的末尾并没有解决它,我的屏幕前面仍然有一个UIWindow,它会窃取所有用户交互。

我的解决方案不是在UnityAppController.mm中创建一个新窗口,而是使用当前应用程序keyWindow。

替换:

    _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];

使用:

    _window = [UIApplication sharedApplication].keyWindow;