dentedpixel / LeanTween

LeanTween is an efficient animation engine for Unity
608 stars 135 forks source link

[Suggestion] OnComplete getting set twice in the end won't run the first one #148

Open CraftedPvP opened 4 years ago

CraftedPvP commented 4 years ago

Hi, I've been having problems recently but I manage to fix it. There wasn't any forum that had this problem so I figured I shared this. I'm setting the OnComplete function twice in the end. Thinking it would concatenate the OnComplete Action parameter.

[SerializeField] CanvasGroup inventory;
float transitionDuraction = 1f, waitTimeBeforeResetting = 2f;
float inventoryAnimationId = -1;
Action reset = () => { inventoryAnimationId = -1; };
Action fadeOut = () => { inventoryAnimationId = LeanTween.alphaCanvas(inventory, 0, transitionDuration).setDelay(waitTimeBeforeResetting).id; };
inventoryAnimationId = LeanTween.alphaCanvas(inventory, 1, transitionDuration).setOnComplete(fadeOut).setOnComplete(reset).id;

Apparently, it didn't work. I had to separate the OnComplete for it to work.

Action fadeOut = () => { inventoryAnimationId = LeanTween.alphaCanvas(inventory, 0, transitionDuration).setDelay(waitTimeBeforeResetting).setOnComplete(reset).id; };
inventoryAnimationId = LeanTween.alphaCanvas(inventory, 1, transitionDuration).setOnComplete(fadeOut).id;

It would be nice if there was a warning that I'm setting it twice or some document saying you'll overwrite it.

carsanwitt commented 3 years ago

Having the same issue, didn't notice it for a bit and now it's cause bugs on my game.

My main issue is that it's a silent issue and it's hard to notice.

I would expect that calling setOnComplete more than once would do either of these :

carsanwitt commented 3 years ago

Wrote a quick workaround, in case anyone needs this:

public class LeanTweenWrapper
{
    public LTDescr Animation { get; }

    public List<Action> OnCompleteCallBacks { get; }

    public bool Executed { get; private set; }

    public LeanTweenWrapper(LTDescr anim)
    {
        Animation = anim;
        anim.setOnComplete(OnComplete);

        OnCompleteCallBacks = new List<Action>();

        Executed = false;
    }

    private void OnComplete()
    {
        if (Executed)
        {
            Debug.LogError("Tried to OnComplete more than once.");
            return;
        }

        OnCompleteCallBacks.ForEach(c => c.Invoke());
        Executed = true;
    }

    public void SetOnComplete(Action callback)
    {
        if (Executed)
        {
            Debug.LogError("Tried to assign callback to already completed anim.");
            return;
        }

        OnCompleteCallBacks.Add(callback);
    }
}