问题描述
我想在为 iOS10+ 设计的应用中包含 ARKit,如果 iOS 版本
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 的设备上运行时通过 AR 显示在现实世界中",并且在完全虚拟的环境中(即,在没有摄像头馈送的情况下以 3D 方式呈现)背景)在没有 ARKit 的情况下运行时.
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.
要做到这一点,您需要能够在运行时换出您的视图类.这意味着不是在故事板中创建 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 时,您必须考虑将相机放在哪里以及如何让用户控制它.(查看 WWDC17 SceneKit session,了解有关新 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 的替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!