Swift 整合统一+;Vuforia到现有iOS项目使用户交互无法工作
目前,我正在跟踪将Unity+Vuforia项目集成到我现有的iOS项目中。我设法在ARViewController中显示Unity视图。问题是我失去了视图控制器中的所有用户交互:我的后退按钮触摸事件没有启动Swift 整合统一+;Vuforia到现有iOS项目使用户交互无法工作,swift,unity3d,vuforia,Swift,Unity3d,Vuforia,目前,我正在跟踪将Unity+Vuforia项目集成到我现有的iOS项目中。我设法在ARViewController中显示Unity视图。问题是我失去了视图控制器中的所有用户交互:我的后退按钮触摸事件没有启动 import Foundation class ARViewController: UIViewController { var unityView: UIView? static func instantiateViewController() -> ARVi
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。两者似乎都无法触及事件。
调试元素:
当我将Unity配置放在
应用程序: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
}
对于视图,将在视图控制器中显示(:)
:
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()
,就会消失(:)
:
当前的问题是,如果我离开屏幕,我就无法杀死unity进程。这是众所周知的,如果可能的话,我仍在想如何做到这一点 我也遇到了同样的问题,但将Unity配置移动到
ApplicationIDFinishLaunchingWithOption
方法的末尾并没有解决这个问题,我的屏幕前仍然有一个UIWindow,它会窃取所有用户交互
我的解决方案不是在UnityAppController.mm
中创建新窗口,而是使用当前的应用程序键窗口
替换:
_window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
与:
什么意思?如果我调试该元素,unity视图(如果包装在UnityDefaultController中)与ARViewControllerI中的我的按钮处于同一级别,我将wikitude与同一教程集成在一起,并且我能够完成我所能做到的一切。我不知道为什么。目前我只需要在导航栏上添加“后退”按钮。我设法做到了。谢谢你的分享,我真的很感激。问题是
handleUnityReady
它只会调用一次!!!所以你的unity视图没有被添加进去,所以我建议你删除那个观察者,视图会出现手动调用那个方法很好,我在前面找到了它,但只是忘了发布!!
_window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
_window = [UIApplication sharedApplication].keyWindow;