using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public enum TweenStyle { Once, Loop, PingPong, } public class TweenPosition : MonoBehaviour { #if UNITY_EDITOR private void Reset() { from = transform.localPosition; to = transform.localPosition; } #endif public Vector3 from; public Vector3 to; public TweenStyle playStyle = TweenStyle.Once; public AnimationCurve curve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f)); public float timer = 1f; private Tween tweener = null; // Use this for initialization void Start() { PlayForward(); } public void PlayForward() { ResetToBegining(); switch (playStyle) { case TweenStyle.Once: tweener = transform.DOLocalMove(to, timer).SetEase(curve); break; case TweenStyle.Loop: tweener = transform.DOLocalMove(to, timer).SetEase(curve).SetLoops(-1); break; case TweenStyle.PingPong: tweener = transform.DOLocalMove(to, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); break; } } public void PlayReverse() { ResetToEnding(); switch (playStyle) { case TweenStyle.Once: tweener = transform.DOLocalMove(from, timer).SetEase(curve); break; case TweenStyle.Loop: tweener = transform.DOLocalMove(from, timer).SetEase(curve).SetLoops(-1); break; case TweenStyle.PingPong: tweener = transform.DOLocalMove(from, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); break; } } public void Stop() { if (tweener != null) { tweener.Kill(); } } public void Pause() { if (tweener != null) { tweener.Pause(); } } public void Play() { if (tweener != null) { tweener.Play(); } } public void ResetToBegining() { if (tweener != null) { tweener.Kill(); } transform.localPosition = from; } public void ResetToEnding() { if (tweener != null) { tweener.Kill(); } transform.localPosition = to; } }