繁体   English   中英

无法使用 RealityKit 场景预览 SwiftUI Canvas

[英]Can't preview SwiftUI Canvas with RealityKit scene

我只创建了 RealityKit 的一个新属性。 Xcode 将无法预览 SwiftUI canvas。 但它可以成功构建。

  1. 我通过 Xcode 创建了应用程序。
  2. 选择增强现实应用程序并将用户界面设置为 SwiftUI
  3. 项目可以正常工作。
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
  1. 我只在makeUIView function 中创建了RealityKit 的新属性。
let test: AnchoringComponent.Target.Alignment = .horizontal
  1. 工程无法预览canvas出现错误

    “Alignment”不是“AnchoringComponent.Target”的成员类型

在此处输入图像描述

  1. 该项目仍然可以编译成功。 在此处输入图像描述

我很困惑我遇到了什么。 有没有人遇到同样的问题?

您必须解决几个问题

  1. 您不能在 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 ModeSwiftUI Preview Mode下无用,而且其他面向会话的属性(包括 ARView.session)也像您可以在图片上看到的那样:

在此处输入图像描述

  1. .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 预览区看到以下内容:

在此处输入图像描述

  1. 当然,在使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM