[英]Can't preview SwiftUI Canvas with RealityKit scene
我只创建了 RealityKit 的一个新属性。 Xcode 将无法预览 SwiftUI canvas。 但它可以成功构建。
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
return VStack {
Text("123")
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
let test: AnchoringComponent.Target.Alignment = .horizontal
“Alignment”不是“AnchoringComponent.Target”的成员类型
我很困惑我遇到了什么。 有没有人遇到同样的问题?
您必须解决几个问题:
- 您不能在 iOS 模拟器或SwiftUI Canvas 预览中使用
anchoring component
,因为它只能用于将虚拟内容固定到现实世界表面。 所以没有 AR 应用程序的模拟器。
RealityKit 锚点在 iOS 模拟器模式和 SwiftUI Canvas 预览模式下无用。
// Use it only for compiled AR app, not simulated...
let _: AnchoringComponent.Target.Alignment = .horizontal
不仅锚点在iOS Simulator Mode
和SwiftUI Preview Mode
下无用,而且其他面向会话的属性(包括 ARView.session)也像您可以在图片上看到的那样:
.backgroundColor
中的 .backgroundColor 更改为任何其他想要的。 默认颜色有时不允许您看到 RealityKit 场景。 看起来像一个错误。
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
boxAnchor.steelBox?.scale = SIMD3(5, 5, 5)
boxAnchor.steelBox?.orientation = simd_quatf(angle: Float.pi/4, axis: SIMD3(1,1,0))
arView.backgroundColor = .orange
arView.scene.anchors.append(boxAnchor)
return arView
}
现在您可以在 SwiftUI 预览区看到以下内容:
- 当然,在使用 AR 应用程序之前,您必须先授予
Camera Permission
。 你使用什么并不重要:Storyboard 或 SwiftUI。
您需要在info.plist
文件中添加Camera Usage Description
属性和arkit
字符串:
XML 版本如下所示:
/* info.plist
<key>NSCameraUsageDescription</key>
<string>Camera access for AR experience</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
<string>arkit</string>
</array>
*/
修复这些问题后,应用程序按预期编译和工作(没有任何错误):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.