首页 > 解决方案 > ARKit 平面检测 - “ARView”类型的值没有成员“会话”

问题描述

我正在尝试将平面检测添加到一个简单的 ARKit 应用程序中。我想在垂直平面上放一张图片。

所以首先我需要检测平面然后我可以添加我在 RealityKit 中创建的对象锚。

但是问题是我不确定使用 ARKit 3 和 Xcode 11 检测飞机并将其添加到我的场景中的正确方法。

它应该很简单:

import ARKit
import RealityKit

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()

    let arConfiguration = ARWorldTrackingConfiguration()
    arConfiguration.planeDetection = .horizontal
    arView.session.run(arConfiguration)
} 

但我收到以下错误:

“ARView”类型的值没有成员“会话”

我什至尝试了以下内容,Apple 在他们的 WWDC 演示 (4:27) 中将其用作示例,

苹果演示

let anchor = AnchorEntity(plane: .verticle, minimumBounds: [0.2, 0.2])
arView.scene.addAnchor(anchor)

但是在尝试创建 AnchorEntity 时出现以下错误

表达式类型“AnchorEntity”在没有更多上下文的情况下是模棱两可的

import UIKit
import RealityKit
import ARKit

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func addFrame() {
        // 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)
    }
}

标签: iosswiftaugmented-realityarkitrealitykit

解决方案


如果您的构建目标设置为模拟器,则会收到此错误。您需要选择实际设备或通用 iOS 设备。

我已经为此撞了几次头,现在添加以下评论

    // If the following line fails to compile "Value of type 'ARView' has no member 'session'"
    // You need to select a Real Device or Generic iOS Device and not a simulator
    arView.session.run(configuration)

推荐阅读