SwiftUI + RealityKit + ARSessionDelegate

I am wondering how I can edit the func session(_ session: ARSession, didAdd anchors: [ARAnchor]) in the arView.session.delegate. I would like to run a function when the image is recognized.


Code Block swiftstruct ARViewWrapper: UIViewRepresentable {  let title: String  typealias UIViewType = ARView     func makeUIView(context: Context) -> ARView {    print("detected?")    let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true)    let target = AnchorEntity(.image(group: "AR Resources", name: "qr1"))    arView.scene.anchors.append(target)    addARObjs(anchor: target, title: title, planeColor: .blue)    return arView  }     func updateUIView(_ uiView: ARView, context: Context) {    print("update!")    return()  }}


Any and all guidance is appreciated! Thanks in advance.
Answered by DTS Engineer in 623604022
Hello,

You should create a Coordinator object to act as your ARSessionDelegate. See the Interfacing with UIKit SwiftUI Tutorial for an example that uses a Coordinator.
I believe that what you want to try is when the anchor state changes on your AnchorEntity.
For that you can subscribe to SceneEvents.AnchoredStateChanged.

an example of that might look like:
Code Block swiftvar eventSub: Cancellable?eventSub = self.scene.subscribe(	to: SceneEvents.AnchoredStateChanged.self,	on: target) { event in	if event.isAnchored {		// image found	}}

You must import the framework Combine to use Cancellable.
Accepted Answer
Hello,

You should create a Coordinator object to act as your ARSessionDelegate. See the Interfacing with UIKit SwiftUI Tutorial for an example that uses a Coordinator.
Sorry I am late but create a an ARSession delegate
SwiftUI + RealityKit + ARSessionDelegate
 
 
Q