AnnulusGames / LitMotion

Lightning-fast and Zero Allocation Tween Library for Unity.

Home Page:https://annulusgames.github.io/LitMotion/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Complete in callback cause unexpected behaviour

Akeit0 opened this issue · comments

CallBack内で自身のhandleをCompleteさせると再帰してスタックオーバーフロー、
他のをhandleをCompleteさせるとOnCompleteActionが二回呼ばれます。

こんな感じの用意した方がいいと思います。

public bool IsCallbackRunning { get;private set; }
public int RunningIndex;
public RunningMaker MarkRunning()=>new (this);
internal readonly struct RunningMaker : IDisposable
{
    public RunningMaker(MotionStorage<TValue, TOptions, TAdapter> storage)
    {
        this.storage = storage;
        storage.IsCallbackRunning = true;
    }

    readonly MotionStorage<TValue, TOptions, TAdapter> storage;

    public void Dispose()
    {
        storage.IsCallbackRunning = false;
    }
}
if(!IsCallbackRunning)
{
    float endProgress = motion.LoopType switch
    {
        LoopType.Restart => 1f,
        LoopType.Yoyo => motion.Loops % 2 == 0 ? 0f : 1f,
        LoopType.Incremental => motion.Loops,
        _ => 1f
    };
    var endValue = default(TAdapter).Evaluate(
        motion.StartValue,
        motion.EndValue,
        motion.Options,
        new() { Progress = EaseUtility.Evaluate(endProgress, motion.Ease) }
    );
    callbacksArray[denseIndex].InvokeUnsafe(endValue);
    callbacksArray[denseIndex].OnCompleteAction?.Invoke();
}
else if(RunningIndex==denseIndex)
{
    callbacksArray[denseIndex].OnCompleteAction?.Invoke();
}

このままで良ければPRします。

おそらくその実装は複数Handleによる循環的な再帰呼び出しに対応できません。現在こちらで修正をほとんど終えているため、PRいただかなくても大丈夫です。

#9 で修正済み