Unity+Vuforia集成现有iOS项目后用户交互失效问题
Unity+Vuforia集成iOS项目后触摸事件全失效求助
兄弟,我正在把Unity+Vuforia项目集成到现有iOS项目中,已经成功在ARViewController里显示Unity视图,但现在所有用户交互都挂了——不管是iOS原生的返回按钮,还是Unity里的按钮,触摸事件完全触发不了,急死了!
我的实现代码(Swift)
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 // 我本来想让Unity视图在原生UI下面,所以直接addSubview了 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) } }
具体现象
- 蓝色的是我在
ARViewController里加的原生按钮,点了没反应 - 绿色栏里的返回按钮是Unity端做的,点了也完全没动静
调试截图
界面展示:
调试元素层级:
我帮你排查后的解决方案
兄弟,这触摸失效的坑我之前踩过好几次,主要是这几个问题导致的,你挨个试:
1. 原生按钮绑定了不存在的方法!
你代码里给按钮绑定的#selector(backPressed)根本没实现!你的VC里只有onBackPressed和backPressedTest,这直接导致原生按钮点了没反应!赶紧改成:
button.addTarget(self, action:#selector(onBackPressed), for: .touchUpInside)
2. 原生按钮约束不全,位置不对!
你只给按钮加了宽度约束,没设置位置(x/y),还没关translatesAutoresizingMaskIntoConstraints,导致按钮可能不在你看得见的区域!修改按钮代码:
let button = UIButton(type: .custom) button.setImage(#imageLiteral(resourceName: "ic_back_black").withRenderingMode(.alwaysTemplate), for: .normal) button.addTarget(self, action:#selector(onBackPressed), for: .touchUpInside) button.translatesAutoresizingMaskIntoConstraints = false // 必须关掉自动布局 button.tintColor = UIColor.purpleBrown() view?.addSubview(button) // 给按钮加位置约束,比如放在左上角安全区 NSLayoutConstraint.activate([ button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16), button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8), button.widthAnchor.constraint(equalToConstant: 28), button.heightAnchor.constraint(equalToConstant: 60) ])
3. Unity视图可能禁用了用户交互
Unity的GLView默认有时会把isUserInteractionEnabled设为false,添加视图时强制开启:
func showUnitySubView() { guard let unityView = UnityGetGLView() else { return } self.unityView = unityView unityView.isUserInteractionEnabled = true // 新增这行 // 原来的添加视图和约束代码... }
4. 修复Unity与原生的事件传递冲突
Unity的UnityAppController会接管应用事件循环,嵌入原生VC时会拦截触摸事件,在ARViewController里重写hitTest方法确保事件正确分发:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { // 优先响应原生控件 let hitView = super.hitTest(point, with: event) if hitView != nil && hitView != unityView { return hitView } // 点击Unity区域则交给Unity处理 return unityView }
5. 检查Unity端Vuforia设置
最后去Unity编辑器确认:
- 打开Vuforia Configuration,确保
Enable Input Handling已勾选 - 检查Unity里的按钮是否正确绑定点击事件,有没有被其他UI遮挡
内容的提问来源于stack exchange,提问作者Seto
相关产品推荐
相关产品推荐

