RealityKit动画完成回调问题
我在RealityKit应用中使用下方的函数来做一些动画,整体来说没问题。
但有一个问题;动画并不总是在给定的持续时间内完成,因此会影响在动画完成后开始的后续事情。
因为我们正在使用的这个动画没有完成回调(我找不到),所以我们采用了如下方式
DispatchQueue.main.asyncAfter(deadline: .now() + duration + 0.1) { [self] in
// next animation
}
我在寻找RealityKit动画的确定性完成回调。
let newTransform = Transform(
scale: currentTransform.scale,
rotation: newRotation,
translation: currentTransform.translation)
entity.move(
to: newTransform,
relativeTo: anchor,
duration: duration,
timingFunction: .easeInOut
)
解决方案
有很多 AnimationEvents 可以订阅。例如,你可以订阅 PlaybackTerminated,
let playbackController = entity.move(
to: newTransform,
relativeTo: anchor,
duration: duration,
timingFunction: .easeInOut
)
for await event in scene.publisher(for: AnimationEvents.PlaybackTerminated.self, on: entity).values {
// check if the playback controller is the same one, in case there are other completed animations
if event.playbackController == playbackController {
break
}
}
// now the animation is over, do other things...
在这里我只是使用 values 将发布者作为异步序列来消费。
我在这里测试了这个答案,下面是我的测试片段;
编译器有点小抱怨,所以做了一个小调整。
let playbackController = entity.move(
to: newTransform,
relativeTo: anchor,
duration: duration,
timingFunction: .easeInOut
)
Task {
for await event in arView.scene.publisher(for: AnimationEvents.PlaybackTerminated.self, on: entity).values {
if event.playbackController == playbackController {
//nextAnimation()
break
}
}
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。