【问题标题】:Preview error with SwiftUI and RealityKit - Value of type 'ARView' has no member 'raycast'SwiftUI 和 RealityKit 的预览错误-“ARView”类型的值没有成员“raycast”
【发布时间】:2020-05-17 19:24:18
【问题描述】:

我在 Xcode 11.4 中预览时遇到了一些问题。我的手机插上电源时我的代码正在工作,所以这不是代码问题,但是拔掉电源时,构建总是失败。我希望能够在不使用 AR 的其他文件上处理我的项目,而不会出现此错误。当我恢复对其他文件的预览时,由于此错误而被阻止。 我已经在 info.plist 文件中添加了一些字符串(隐私摄像头使用和所需的设备功能),但仍然无法正常工作。有想法吗?

import SwiftUI
import RealityKit


struct ContentView : View {
    var body: some View {

         return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        arView.enablePlacement()

        return arView

    }

    func updateUIView(_ uiView: ARView, context: Context) {}

}

extension ARView {
    func enablePlacement() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
        self.addGestureRecognizer(tapGestureRecognizer)
    }

    @objc func handleTap(recognizer: UITapGestureRecognizer) {
        let location = recognizer.location(in: self)

        let results = self.raycast(from: location, allowing: .estimatedPlane, alignment: .vertical)

       if let firstResult = results.first {
            let mesh = MeshResource.generateBox(width: 0.5, height: 0.02, depth: 0.2)
            var material = SimpleMaterial()
            material.baseColor = try! MaterialColorParameter.texture(TextureResource.load(named: "glacier"))
            let modelEntity = ModelEntity(mesh: mesh,materials: [material])
            let anchorEntity = AnchorEntity(world: firstResult.worldTransform)
            anchorEntity.addChild(modelEntity)
            self.backgroundColor = .orange
            self.scene.addAnchor(anchorEntity)

        }else{
            print("No Surface detected - move around device")
        }
    }
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

错误类型“ARView”的值没有成员“raycast”。

无法根据成员“estimatedPlane”推断上下文基础。

无法根据成员“垂直”推断上下文基础。

Screen Capture

【问题讨论】:

  • 嗨,吉姆,欢迎来到 SO!您应该在帖子本身中输入错误而不是链接它,因为链接可能随时关闭,并且不允许未来与您有类似问题的人获得有关正在发生的事情的正确上下文。

标签: swift xcode swiftui arkit realitykit


【解决方案1】:

很多 RealityKit 符号在模拟器中不可用。我认为您唯一的解决方案是使用

从模拟器构建中删除它们
#if !targetEnvironment(simulator)
/ ** /
#endif

【讨论】:

    【解决方案2】:

    出现此错误是因为您在模拟器中选择了设备,而模拟器无法与 AR 应用一起使用。将 Xcode 顶部播放和停止按钮旁边的下拉菜单更改为 Any iOS Device (arm64)

    您需要将物理设备连接到 Xcode 或将您的应用推送到 AppStoreConnect 并使用 Testflight 而不是模拟器来测试您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2020-02-13
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2018-03-14
      相关资源
      最近更新 更多