【问题标题】:Integrating Unity+Vuforia to existing iOS project make user interaction not working将 Unity+Vuforia 集成到现有 iOS 项目使用户交互无法正常工作
【发布时间】:2018-05-18 04:51:46
【问题描述】:

目前我正在关注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。两者似乎都没有达到触摸事件。

调试元素:

【问题讨论】:

  • 什么意思?如果我调试元素,统一视图是否包含在 UnityDefaultController 中,并且与我在 ARViewController 中的按钮处于同一级别
  • 我已经将wikitude与相同的教程集成在一起,我可以做任何事情
  • 我设法让它工作。我不知道为什么。
  • 目前我只需要在我的导航栏上添加返回按钮。我设法做到了。感谢您的分享,我真的很感激。
  • 问题在于handleUnityReady,它只会调用一次!!!所以你的统一视图没有添加,所以我建议你删除那个观察者,视图会出现手动调用该方法

标签: swift unity3d vuforia


【解决方案1】:

当我将 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,如果可能的话,我仍在研究如何做到这一点。

【讨论】:

  • 干得好,我很早就找到了,但忘了发帖!!
【解决方案2】:

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

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

替换:

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

与:

    _window = [UIApplication sharedApplication].keyWindow;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2018-09-21
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多