1

Simply trying to print a hello when I tap on an object in the Reality file I made in Reality Composer. Not able to set the link between Notify and in-app actions.

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        ZStack{
            ARViewContainer()
            Text("Level 1")
        }
    }
}

struct ARViewContainer : UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let yellowEntity = try! ModelEntity.load(named: "Yellow")
        let anchorEntity = AnchorEntity(plane: .horizontal)
        anchorEntity.addChild(yellowEntity)
        arView.scene.addAnchor(anchorEntity)
        yellowEntity.actions.Yellowtapped.onAction = handleTap(_:)
        func handleTap(_entity: Entity?){
            guard let entity = entity else {return}
            print("Hello")
        }
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
BGG
  • 11
  • 1

1 Answers1

1

If you use .rcproject all works perfectly:

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        let scene = try! Experience.loadBox()
        
        scene.actions.notifier.onAction = printer
        
        let anchor = AnchorEntity(plane: .horizontal)
        anchor.addChild(scene)
        arView.scene.addAnchor(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
    
    func printer(_ entity: Entity?) -> Void { print("Hello") }
}

enter image description here

P.S.

Do not forget to merge 2 actions together (in Reality Composer).

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220