95 lines
2.3 KiB
C#
95 lines
2.3 KiB
C#
using System;
|
|
using DG.Tweening;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
public class DOTweenAction : IAction
|
|
{
|
|
private DOTweenAction()
|
|
{
|
|
}
|
|
|
|
private static readonly SimpleObjectPool<DOTweenAction> Pool = new(() => new DOTweenAction(), null, 10);
|
|
|
|
private Func<Tween> _tweenGetter;
|
|
|
|
public static DOTweenAction Allocate(Func<Tween> tweenGetter)
|
|
{
|
|
var doTweenAction = Pool.Allocate();
|
|
doTweenAction.ActionID = ActionKit.IDGenerator++;
|
|
doTweenAction.IsFinalized = false;
|
|
doTweenAction.Reset();
|
|
doTweenAction._tweenGetter = tweenGetter;
|
|
return doTweenAction;
|
|
}
|
|
|
|
private Tween _executingTween;
|
|
|
|
public void OnStart()
|
|
{
|
|
_executingTween = _tweenGetter();
|
|
_executingTween.Restart();
|
|
|
|
var tween = _tweenGetter();
|
|
tween.Restart();
|
|
}
|
|
|
|
public void OnExecute(float dt)
|
|
{
|
|
if (_executingTween != null && !_executingTween.IsPlaying())
|
|
{
|
|
this.Finish();
|
|
}
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
_executingTween = null;
|
|
}
|
|
|
|
public bool Paused { get; set; }
|
|
|
|
public void Deinit()
|
|
{
|
|
if (!IsFinalized)
|
|
{
|
|
IsFinalized = true;
|
|
if (_executingTween != null)
|
|
{
|
|
if (_executingTween.IsPlaying())
|
|
{
|
|
_executingTween.Kill();
|
|
}
|
|
}
|
|
|
|
_executingTween = null;
|
|
Pool.Recycle(this);
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Status = ActionStatus.NotStart;
|
|
Paused = false;
|
|
_executingTween = null;
|
|
}
|
|
|
|
public bool IsFinalized { get; set; }
|
|
public ulong ActionID { get; set; }
|
|
public ActionStatus Status { get; set; }
|
|
}
|
|
|
|
public static class ActionKitDOTweenExtension
|
|
{
|
|
public static IAction ToAction(this Tween self)
|
|
{
|
|
self.Pause();
|
|
return DOTweenAction.Allocate(() => self);
|
|
}
|
|
|
|
public static ISequence DOTween(this ISequence self, Func<Tween> tweenGetter)
|
|
{
|
|
return self.Append(DOTweenAction.Allocate(tweenGetter));
|
|
}
|
|
}
|
|
} |