Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.34k stars 351 forks source link

Not possible to kill tweens with OnDestroy #596

Closed piscopancer closed 2 years ago

piscopancer commented 2 years ago
 void OnDestroy()
    {
        tweenBlink?.Kill();
        tweenSpin?.Kill();
    }

the safe mode is yelling at me that there are still images in my menu panel that spin and blink and I kind of cut them off from working process and so on. Why?

Demigiant commented 2 years ago

I can confirm that Kill always works, so maybe those tweens are not referenced to those vars? Also, are you sure OnDestroy is called? If your MonoBehaviour never went through Awake it will not go through OnDestroy.

piscopancer commented 2 years ago

I can confirm that Kill always works, so maybe those tweens are not referenced to those vars? Also, are you sure OnDestroy is called? If your MonoBehaviour never went through Awake it will not go through OnDestroy.

I am doing something wrong here. I put all tweens in the list and when my panel is closed, I kill all of them. But...

[RequireComponent(typeof(CanvasGroup))]
public class PanelBase : MonoBehaviour
{
    const float TIME_ANIM_OPEN = 0.15f;

    public static Action<PanelBase> OnOpened, OnClosed;

    protected List<Tween> ListTweens = new List<Tween>();

    protected virtual void Awake()
    {
        OnOpened?.Invoke(this);
        var tweenOpen = GetComponent<CanvasGroup>().DOFade(1, TIME_ANIM_OPEN).From(0);
        ListTweens.Add(tweenOpen);
    }

    public virtual void Close()
    {
        OnClosed?.Invoke(this);
        if (ListTweens.Count > 0)
        {
            foreach (var tween in ListTweens)
            {
                tween?.Kill();
            }
        }
        Destroy(gameObject);
    }
}
Demigiant commented 2 years ago

I'm not sure what could be the problem there. Could you add logs to be sure that Close is called, and that ListTweens contains something? P.S. Also, you can remove the null check before Kill: it's an extension method that already checks for null internally

piscopancer commented 2 years ago

I have debugged it before posting it here heh, the List actually contains all tweens as I expected and Close() is definitely called. The problem is in the interaction between PanelSettings class and its parent PanelBase -> PanelSettings has buttonClose that calls Close() in PanelBase...

Long story short...

PanelSettings: https://github.com/piscopancer/LockedUpGunfight/blob/ab957b862b60d1f8aa21d3452a5df105e6e2eefa/%E2%80%94%20Scripts/UI/PanelSettings.cs

PanelBase: https://github.com/piscopancer/LockedUpGunfight/blob/ab957b862b60d1f8aa21d3452a5df105e6e2eefa/%E2%80%94%20Scripts/UI/PanelBase.cs

I will be +inf grateful to you if you can tell what the shit wrong I am doing here bcs (!) I feel like I am doing all the stuff correctly but in fact I catch errors all the time.

(I don't want to be a hard rock on your shoulders so I am gonna spend one more night now reading your docs and code of mine <3 )

piscopancer commented 2 years ago

image

it is somehow fascinating the program is giving off errors of DOFade but keeps working...

piscopancer commented 2 years ago

I have fixed it. The problem was I did not declare tween fields for tweens that were active so they were not in OnDestroy(). The solution was on the surface...