DoTween で Tween
を再利用するために下記のようなコードを書いたところ
1度しか再生できないという現象が発生した
using UnityEngine; using DG.Tweening; using UnityEngine.UI; public class SampleMonoBehaviour : MonoBehaviour { [SerializeField] private Transform target; [SerializeField] private Button button; private void Awake() { var tween = target.DOLocalMove(new Vector3(1f, 1f, 0f), 1f) .SetRelative() .Pause(); button.onClick.AddListener(() => { tween.Restart(); }); } }
公開されている Github からコードを漁って調べてみたところ
Complete 時に autoKill が true ならば Despawn が呼ばれ Tweener がリセットされていた
internal static bool Complete(Tween t, bool modifyActiveLists = true, UpdateMode updateMode = UpdateMode.Goto) { ・・・ if (t.autoKill) { if (isUpdateLoop) t.active = false; // Just mark it for killing, so the update loop will take care of it else Despawn(t, modifyActiveLists); } ・・・ }
autoKill は 設定を見たところ、デフォルトが true になっていたので、再利用する場合はこれを false にする必要がある
全体の autoKill を false にするとゴミがたまりそうなので個別に設定する
個別に設定するためには SetAutoKill(false)
を追加してやればよい
var tween = target.DOLocalMove(new Vector3(1f, 1f, 0f), 1f) .SetRelative() .Pause() .SetAutoKill(false);
これで何度も 再生できるようになった