问题描述
我想在一个专为iOS10 +设计的应用程序中包含ARKit,如果iOS版本为< 11,我将用SceneKit替换ARKit。
I want to include ARKit in an app designed for iOS10+ where I replace ARKit with SceneKit if the iOS version is <11.
不幸的是,似乎有目前无法做到这一点?
Unfortunately it seems like there is no way to currently do this?
推荐答案
根据您的期望设定,可以将SceneKit用作来自ARKit的后备 - 特别是来自 ARSCNView
。
Depending on how you've set your expectations, it is possible to use SceneKit as a "fallback" from ARKit -- specifically, from ARSCNView
.
您可以轻松做的是创建3D内容在没有ARKit的情况下运行时,在支持ARKit的设备上运行时通过AR显示在现实世界中的体验,以及在没有相机提要作为背景的情况下以3D形式呈现的体验。
What you can easily do is create a 3D content experience that appears "in the real world" via AR when running on an ARKit capable device, and in an entirely virtual setting (that is, rendered in 3D without a camera feed as background) when running without ARKit.
To这样做,你需要能够在运行时换出你的视图类。这意味着不在故事板中创建 ARSCNView
,而是初始化一个并将其添加到代码中的视图控制器的根视图中。
To do that, you'll need to be able to swap out your view class at runtime. That means not creating ARSCNView
in a storyboard, but initializing one and adding it to your view controller's root view in code.
override func viewDidLoad() {
super.viewDidLoad()
let sceneView = ARSCNView(frame: view.bounds)
sceneView.scene = // ... set up your SceneKit scene ...
view.addSubview(sceneView)
}
一旦你做了类似的事情,你可以将关键部分包装在一个条件中,该条件在可用时使用 ARSCNView
并且 SCNView
否则。实际上,你可能想为它设置一个方便的功能......
Once you're doing something like that, you can wrap the critical part in a conditional that uses ARSCNView
when available and SCNView
otherwise. Actually, you might want to set up a handy function for that...
var canUseARKit: Bool {
if #available(iOS 11.0, *) {
return ARWorldTrackingSessionConfiguration.isSupported
} else {
return false
}
}
然后您可以条件化您的视图设置:
Then you can conditionalize your view setup:
override func viewDidLoad() {
super.viewDidLoad()
let sceneView: SCNView
if canUseARKit {
sceneView = ARSCNView(frame: view.bounds)
} else {
sceneView = SCNView(frame: view.bounds)
}
sceneView.scene = // ... set up your SceneKit scene ...
view.addSubview(sceneView)
}
当然,之后还有更多工作要做:
Of course, there's still more to do after that:
- 当您使用ARKit时,您可以免费获得相机控制;当你自己使用SceneKit时,你必须考虑放置相机的位置以及如何让用户控制它。 (查看,了解有关新iOS 11相机控件的一些提示。)
- 当您自己使用SceneKit时,您的3D内容定义了用户与之交互的世界 - 也就是说,您将相机等与您的相关联内容。使用ARKit时,您必须考虑在何处以及如何将内容相对于现实世界。
- When you're using ARKit, you get camera control "for free"; when you're using SceneKit on its own you'll have to think about where to place the camera and how to let the user control it. (Check the WWDC17 SceneKit session for some tips on new iOS 11 camera controls.)
- When you're using SceneKit on its own, your 3D content defines "the world" that the user interacts with -- that is, you place cameras and such in relation to your content. When using ARKit, you have to think about where and how to place your content relative to the real world.
但除此之外,使用 ARSCNView
中的SceneKit内容与使用 SCNView
中的SceneKit内容没什么不同,所以对于你的其余部分应用程序/游戏,您可以在AR和非AR用户体验之间共享大量代码和资产。
But aside from such concerns, working with SceneKit content within ARSCNView
is no different from working with SceneKit content in SCNView
, so for the rest of your app/game you can share a lot of code and assets between AR and non-AR user experiences.
这篇关于在iOS10中替换ARKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!