Replies: 1 comment 4 replies
-
Hey! I hope you're enjoying PrimeTween :) There is no OnStop() callback for simplicity reasons. I understand the need for your specific use case, but I'm afraid you're using PrimeTween in a very inefficient way. You allocate GC memory at least four times in your code snippet: 1 - tween is captured in a closure by the onTimeScaleChanged delegate; 2 - tween.OnComplete() captures the timeScaleObject; 3 - every time you subscribe or unsubscribe to OnTimeScaleChanged event. This can trump the performance of your project very quickly. For example, this simple alternative implementation of TimeScaleObject addresses the missing OnStop() method, while also drastically improving performance: public class TimeScaleObject {
const int initialCapacity = 20;
readonly List<Tween> tweens = new List<Tween>(initialCapacity);
/// Add tweens to the TimeScaleObject to control their timeScale together
public void AddTween(Tween tween) {
for (int i = 0; i < tweens.Count; i++) {
if (!tweens[i].isAlive) {
// replace the first encountered 'dead' tween with a new one
tweens[i] = tween;
return;
}
}
tweens.Add(tween);
}
/// Apply timeScale to all added tweens at once
public void SetTimeScale(float timeScale) {
for (int i = tweens.Count - 1; i >= 0; i--) {
if (tweens[i].isAlive) {
tweens[i].timeScale = timeScale;
} else {
// remove all 'dead' tweens
tweens.RemoveAt(i);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Sorry if the text is difficult to understand as I am using a translator.
Thank you for your wonderful library. Very comfortable to use!
By the way, is there an OnStop() callback?
In my project, I am managing TimeScale for enemy animations, UI animations, etc. separately.
So I have the following extension method to change the TimeScale dynamically.
However, Tween.OnComplete only works when the tween completes automatically or when Complete() is called, so if you use Tween.Stop() to stop it, the OnTimeScaleChanged event will remain registered. Therefore, a Tween.OnStop() callback corresponding to Tween.Stop() is required.
It would also be useful to be able to register more than one function in a callback, such as OnComplete, since there are times when you want to use each callback in the FollowTimeScale extension and in the project code.
Beta Was this translation helpful? Give feedback.
All reactions