I'm trying to create a function that pauses AR session and also clears my dictionary variable entityDictionaries
and removes all anchors. However, I'm kinda stuck and don't know how to start.
Here is a code for my main view:
struct InitiateARView: View {
@StateObject var vm = ARPreparationViewModel()
var body: some View {
VStack {
if vm.isLoading {
Button("Start", action: {
vm.prepareAR()
})
} else {
ZStack {
ARExperience()
.edgesIgnoringSafeArea(.all)
VStack {
HStack {
Spacer()
Button("Stop AR", action: {
// Call to clear AR session function here
})
}
Spacer()
}
}
}
}
}
}
And here is simplified version of my ARExperience struct
struct ARExperience: UIViewRepresentable {
var arView = ARView(frame: .zero)
var entityDictionaries = [String: (anchor: AnchorEntity, model: ModelEntity)]()
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
class Coordinator: NSObject, ARSessionDelegate {
var parent: ARExperience
init(parent: ARExperience) {
self.parent = parent
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let validAnchor = anchors[0] as? ARImageAnchor else { return }
let anchor = AnchorEntity(anchor: validAnchor)
let videoPlane = createdVideoPlayerNodeFor(validAnchor.referenceImage)
anchor.addChild(videoPlane)
parent.arView.scene.addAnchor(anchor)
if let targetName = validAnchor.referenceImage.name {
parent.entityDictionaries[targetName] = (anchor, videoPlane)
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
}
}
func makeUIView(context: Context) -> ARView {
//...
}
func updateUIView(_ uiView: ARView, context: Context) {
}
}
Any help that can get me 'unstuck' with this problem will be perfect