Created
January 16, 2024 01:54
-
-
Save drewolbrich/f900bef53a3b3e9218d5bbfb967536b4 to your computer and use it in GitHub Desktop.
Failed attempt #1 at Entity/move with a callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import RealityKit | |
import Combine | |
private var playbackCompletedSubscriptions: Set<AnyCancellable> = .init() | |
extension Entity { | |
/// Animated `Entity/move` with a completion handler. | |
/// | |
/// This implementation doesn't work. The animation doesn't happen, | |
/// although `completion` is called after `duration` seconds. | |
func move2(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) { | |
guard scene != nil else { | |
completion() | |
return | |
} | |
let animationPlaybackController = move(to: transform, relativeTo: referenceEntity, duration: duration, timingFunction: timingFunction) | |
scene?.publisher(for: AnimationEvents.PlaybackCompleted.self) | |
.filter { $0.playbackController == animationPlaybackController } | |
.sink(receiveValue: { event in | |
completion() | |
}).store(in: &playbackCompletedSubscriptions) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment