【发布时间】: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,它只会调用一次!!!所以你的统一视图没有添加,所以我建议你删除那个观察者,视图会出现手动调用该方法