I want to make sure that all tweens are killed on my game object just before it gets pooled for recycling.
Is there a function like LeanTween.cancel(gameObject)?
Many thanks!
Hi,
DOTween has no concept of gameObjects, because you can tween stuff that is not related to them. But... when you use shortcuts, the target of your tween is added as the tween's target (for example, a myTransform.DOMove adds "myTransform" as the target). You can then call DOTween.Kill(myTarget) to kill all tweens on that specific target. Otherwise, when creating a tween, you can add the gameObject as the ID of that tween, by chaining a SetId(myGameObject), and then call DOTweenKill(myGameObject), which will kill all tweens with the same ID (in this case a gameObject, but you can also set an int or a string).
Alternatively, if you use safe-mode (beware that it's available on iOS and Win Phone only under certain build conditions), if a tween is running and encounters a missing target, it will delete itself automatically.
If it may help, my workflow is to either store a reference to each tween and call myTween.Kill inside a gameObject's OnDestroy/OnDisable, or use the aforementioned ID, depending on which one is more suitable for the amount of tweens I'm creating.
Cheers,
Daniele
Thanks for the information and advice; I will have a play with .SetId; I wasn't aware that I could specify custom identifiers for my tweens.
Failing that; I might try to employ some sort of tween lifetime manager component for each of my game objects which I can just nuke in one go since some of my objects end up having quite a number of tweens active at the same time. Perhaps something like this:
var tweenLifetimeManager = this.GetComponent<TweenLifetimeManager>();
...
tweenLifetimeManager.Record(this.transform.DOSomething(...));
And then my pooling script can do something like this:
var tweenLifetimeManager = this.GetComponent<TweenLifetimeManager>();
if (tweenLifetimeManager != null) {
tweenLifetimeManager.Kill(complete = false);
}
Thanks again for a wonderfully fast and detailed response!
Cool! I would recommend using the SetId logic. TweenLifeTimeManager is interesting, but it requires adding another component to your target, and the less components the better (you know, GC). Otherwise, you could use a static dictionary or something like that, which is cleaner.
Is it reasonable to use the same ID for many tweens so that all tweens associated with the ID can be killed in one swoop?
I just tested this with the following component; and it seems to work, but I don't want to go against the grain and do things that aren't strictly intended/documented:
using DG.Tweening;
using UnityEngine;
public class MultiTweenKiller : MonoBehaviour
{
private void Start()
{
this.transform.DOScale(10f, 20f).SetId(this.gameObject);
this.transform.DOMoveX(10f, 20f).SetId(this.gameObject);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
DOTween.Kill(this.gameObject);
}
}
}
Absolutely yes, that's the main reason for ID: group operations :)
Consider that all static methods accept an optional ID (Play/Rewind/etc), not just Kill.
Haha! awesome. Okay that makes things considerably easier.
Thanks for such a great tool!! and amazing support.
It's a pleasure! :)