将Unity+Vuforia集成到现有的iOS项目中,导致用户交互失效。

3

目前我正在按照这个教程将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

你的意思是什么?如果我调试该元素,Unity 视图将被包装在 UnityDefaultController 中,并与 ARViewController 中的按钮处于同一级别。 - Seto
我已经按照同样的教程将Wikitude集成了进来,现在我能够完成所有操作。 - Prashant Tukadiya
你解决了你的问题吗? - Prashant Tukadiya
1
目前我只需要在导航栏上添加返回按钮。我已经成功做到了。感谢您的分享,我非常感激。 - Seto
1
问题出在handleUnityReady上,它只会被调用一次!!!因此您的Unity视图没有添加,我建议您删除该观察者并手动调用该方法以使视图出现。 - Prashant Tukadiya
显示剩余8条评论
2个回答

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通知只会被调用一次。 因此我不使用它。

然后我只需在viewWillDisappear(_:)中调用stopUnity()

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

当前的问题是当我离开屏幕时无法终止Unity进程。这是已知的bug,我仍在努力弄清楚如何解决它,如果可能的话。


1
干得好,我早就找到了,只是忘记发布了!! - Prashant Tukadiya

2

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

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

替换:

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

使用:

    _window = [UIApplication sharedApplication].keyWindow;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接