With Reality Composer's notifications you can implement two scenarios:
Action listener
This is your case and it's easy to implement using
public var onAction: ((RealityKit.Entity?) -> Swift.Void)?
.
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let scene = try! Experience.loadScene()
override func viewDidLoad() {
super.viewDidLoad()
arView.scene.anchors.append(scene)
scene.actions.attackCompleted.onAction = notificationID // listener
}
fileprivate func notificationID(_ entity: Entity?) {
print(scene.actions.attackCompleted.identifier)
}
}
Here is one more example of how .onAction
completion handler can be used.
Trigger for action
When you need to notify Reality Composer's scene to play an action use the following scenario:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let scene = try! Experience.loadScene()
override func viewDidLoad() {
super.viewDidLoad()
arView.scene.anchors.append(scene)
}
@IBAction func press(_ sender: UIButton) {
scene.notifications.spinner.post() // trigger for action
}
}
or use a subscript for [NAME.NotificationTrigger]
:
@IBAction func press(_ sender: NSButton) {
scene.notifications.allNotifications[0].post()
}
Here's one more example of how .post() instance method can be used.
P. S.
If you need more info, read this post.