Is there a way to execute code when a Tween or Sequence starts (OnStart equivalent)? #90
-
|
I'm making an animator and i have some specific events that can't we tweened because it makes no sense (i.e changing the sprite of a GameObject, running some code at a specific time etc). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
This question is closely tied to the one about animation caching: #88 Animation caching is possible with PrimeTween as described in the link above, but that's not a recommended way of playing animations. Instead, a simpler approach is to create an animation at the exact moment you need it. So if you do it the recommended way, then the question about OnStart() callback goes away because you can just call a method right before you start the animation: DoSomethingBeforeAnimation();
Tween.Position(...);But if you still want to go in the path of caching animations, then you can chain a callback before other animations in a Sequence. This will have the same effect as DOTween's OnStart() callback: var seq = Sequence.Create()
.ChainCallback(() => DoSomethingBeforeAnimation())
.Chain(Tween.Position(...))
seq.isPaused = true; // unpausing the animation later will call the DoSomethingBeforeAnimation() before starting animation |
Beta Was this translation helpful? Give feedback.
-
|
I know this is a year old, but I ran into this issue today and I don't think your answer fully covers use cases for an OnStart event. For simple purposes, ChainCallback would work, however during Sequences and using Group, it does not work because it breaks the Group. For instance. I want to play a sound effect for each row of a table that appears, and these are setup using a Sequence and Group (with delay offsets) so that they animated in together with some slight stagger. Right now there doesn't appear to be any way to GroupCallback so you are really only left with using OnUpdate and trying to make sure it only triggers the sound effect once using the update progress. I think having the OnStart event is a lot more intuitive than requiring a ChainCallback since it fits nicely alongside the OnComplete event as well. The other alternative to the Sequence + Group combo is nesting a Sequence to support the ChainCallback, which really just feels like overkill for something this simple. |
Beta Was this translation helpful? Give feedback.
This question is closely tied to the one about animation caching: #88
Animation caching is possible with PrimeTween as described in the link above, but that's not a recommended way of playing animations. Instead, a simpler approach is to create an animation at the exact moment you need it. So if you do it the recommended way, then the question about OnStart() callback goes away because you can just call a method right before you start the animation:
But if you still want to go in the path of caching animations, then you can chain a callback before other animations in a Sequence. This will have the same effect as DOTween's OnStart() callback: