Created
January 16, 2024 01:55
-
-
Save drewolbrich/bb1cf4e7e75d61c8bee5d2ad37b9c8c4 to your computer and use it in GitHub Desktop.
Failed attempt #2 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 works but for some reason `timingFunction` appears to be | |
/// ignored and the timing function of the resulting animation is always linear. | |
func move1(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) { | |
guard scene != nil else { | |
completion() | |
return | |
} | |
let animation = FromToByAnimation(name: "Entity/move", to: target, duration: duration, timing: timingFunction, isAdditive: false, bindTarget: .transform, delay: delay) | |
do { | |
let animationResource: AnimationResource = try .generate(with: animation) | |
let animationPlaybackController = playAnimation(animationResource) | |
scene?.publisher(for: AnimationEvents.PlaybackCompleted.self) | |
.filter { $0.playbackController == animationPlaybackController } | |
.sink(receiveValue: { event in | |
completion() | |
}).store(in: &playbackCompletedSubscriptions) | |
} catch { | |
assertionFailure("Could not generate animation: \(error.localizedDescription)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment