Search code examples
swiftuirealitykitvisionosreality-composer-pro

Send Notification from SwiftUI Xcode 16 to trigger Reality Composer Pro Timeline Action Animation


I am using Reality Composer Pro 2.0 and Xcode 16 beta for VisionOS 2 Beta.

I am able to see and trigger RCP Animation Component animations in a RealityView in Xcode by calling playAnimation on scene.availableAnimations[0] when a swiftui button is tapped, all good there.

I want to be able to trigger new Timeline Action animations from tapping a swiftui button as well, but I'm not sure the best way to access Timeline Actions to trigger them in code.

I see that I can send a Notification from Xcode that will trigger a timeline action (as shown in screenshot), but I'm not sure how to send a notification from SwiftUI to RCP to trigger this "SpinAndMove" Timeline animation. enter image description here

If anyone could help send a notification from SwiftUI to RCP or if there's a better "best practices" method to trigger Timeline Actions from outside of RCP, it would be much appreciated!


Solution

  • This was answered on the Apple developer forum: https://forums.developer.apple.com/forums/thread/756978

    That post describes how to do the notification with VisionOS, but I was able to get it to work on a macOS target with:

    if let scene = appModel.contentEntity?.scene {
      NotificationCenter.default.post(
        name: Notification.Name("RealityKit.NotificationTrigger"),
        object: nil,
        userInfo: [
          "RealityKit.NotificationTrigger.Scene": scene,
          "RealityKit.NotificationTrigger.Identifier": "insideFlip"
        ])
    }
    

    Where insideFlip was the Notification Name defined in Reality Composer Pro, and appModel.contentEntity?.scene was defined inside RealityView.

    if let immersiveContentEntity =
        try? await Entity(named: "Immersive", in: realityKitContentBundle) {
      appModel.contentEntity = immersiveContentEntity
    }