Search code examples
swiftswiftuirealitykitreality-composer

Can I pause the notifications in Reality Composer?


In Reality Composer, I identified the notification called replay.

enter image description here

In Xcode, I controlled the notification by adding this code:

Scene1.notifications.replay.post()

Can I call the method for stopping or pausing this notification?


Solution

  • In Reality Composer you can create as many notifications as you want. Thus you can create an empty notification that will not trigger the Behavior when it is posted. The example below shows how every even click does not trigger an animation.


    enter image description here

    boxScene.notifications.none.post()
    

    enter image description here

    SwiftUI version

    import SwiftUI
    import RealityKit
    
    struct ContentView : View {
        
        @State private var counter: Int = 0
        @State private var boxScene = try! Experience.loadBox()
        
        var body: some View {
            ZStack {
                ARViewContainer(boxScene: $boxScene).ignoresSafeArea()
                VStack {
                    Spacer()
                    Button("Tap me") {
                        counter += 1
                        
                        if (counter % 2) == 1 {
                            boxScene.notifications.theMask.post()
                        } else {
                            boxScene.notifications.none.post()
                        }
                    }
                }
            }
        }
    }
    

    struct ARViewContainer: UIViewRepresentable {
        
        let arView = ARView(frame: .zero)
        @Binding var boxScene: Experience.Box
        
        func makeUIView(context: Context) -> ARView {
            arView.scene.anchors.append(boxScene)
            return arView
        }
        func updateUIView(_ view: ARView, context: Context) { }
    }
    

    UIKit version

    import UIKit
    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        let boxScene = try! Experience.loadBox()
        var counter = 0
        
        override func viewDidLoad() {
            super.viewDidLoad()
            arView.scene.anchors.append(boxScene)
        }
        
        @IBAction func tapMe(_ sender: UIButton) {
            counter += 1
            
            if (counter % 2) == 1 {
                boxScene.notifications.theMask.post()
            } else {
                boxScene.notifications.none.post()
            }
        }
    }