Hi! Im making project with Xcode and Reality Composer Pro. I'm trying to play timeline in Reality Composer Pro using codes without setting Behaviors on entities. And I also tried to send notification from Xcode to entities in Reality Composer Pro to play timeline(I already set "OnNotification" with Behaviors component). But it's not working well, and I couldn't figure out any problems. Are there solutions about it?
Hi @PLAY4 ,
Sending notifications to Entities you've set up in Reality Composer Pro requires those Entities to have a Behaviors component. Can you verify for me that your Entities have a Behaviors component? Additionally, there are two kinds of Behaviors you must trigger yourself in code, OnTap
(in response to a tap gesture) and OnNotification
. The other Behaviors, OnCollision
and OnAddedToScene
, do not require you to write anything in code.
If you are using OnTap
, you must manually call applyTapForBehavior()
on the entity your user tapped (I have forgotten to do this myself many times!). For example, you can add this .gesture
modifier to your RealityView
:
.gesture(TapGesture().targetedToAnyEntity().onEnded { value in
_ = value.entity.applyTapForBehaviors()
})
If you are using OnNotification
, you will need to post a notification to NotificationCenter
with the correct information. Check out this forum question from another user for an explanation of how to do that. To rephrase that answer here, you will need a reference to your scene and the name of the notification you set on the Behaviors
component in RCP. Note that your "scene" in RealityKit can be obtained using a property with an environment attribute on your View
:
@Environment(\.realityKitScene) var scene
Then later in your code, you can trigger a notification like this:
NotificationCenter.default.post(
name: Notification.Name("RealityKit.NotificationTrigger"),
object: nil,
userInfo: [
"RealityKit.NotificationTrigger.Scene": scene,
"RealityKit.NotificationTrigger.Identifier": "MyNotification"
]
)
There's one more gotcha you should watch out for: NotificationCenter
is not immediately available in your RealityKit
scene, and any notification, including the one above, will fail silently until NotificationCenter
is ready. This can be an issue when creating simple apps for test purposes, since you are likely sending this notification immediately. If you are still having issues after double-checking your notification identifier and scene variables are correct, try creating a task modifier on your view with a one second delay before sending your notification.
You can download a working (and more complex) example of notifications in Composing interactive 3D content with RealityKit and Reality Composer Pro. Additionally, this WWDC video goes over Reality Composer Pro timelines in depth, and specifically mentions the OnNotification
behavior around the 16:50 mark.
Let me know if you have any questions!