I've got some trouble having a preview in Xcode 11.4. My code is working when my phone is plugged, so it's not a code problem, but when unplugged, build always failed. I'd like to be able to work on my project, on the other files not using AR, without this error. When I resume the preview on those other files, I'm blocked because of this error. I've already put some strings in the info.plist file (privacy camera usage and required device capabilities) but still not working. Have an idea ?
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
error value of type 'ARView' has no member 'raycast'.
Cannot infer contextual base in reference to member 'estimatedPlane'.
Cannot infer contextual base in reference to member 'vertical'.