using System.Collections; using UnityEngine; // Documentation text normalized. public class SimpleAxisTween : MonoBehaviour { public enum Axis { X, Y } // Documentation text normalized. [Header("Tween Settings")] public Axis axis = Axis.X; // Documentation text normalized. public float from = 0f; public float to = 0f; [Min(0.01f)] public float duration = 0.35f; public AnimationCurve ease = AnimationCurve.EaseInOut(0, 0, 1, 1); private Coroutine tweenCo; private RectTransform rt; public void PlayTween() { if (tweenCo != null) StopCoroutine(tweenCo); tweenCo = StartCoroutine(TweenRoutine()); } private void Awake() { TryGetComponent(out rt); } private IEnumerator TweenRoutine() { float elapsed = 0f; SetPosition(from); while (elapsed < duration) { elapsed += Time.unscaledDeltaTime; float t = Mathf.Clamp01(elapsed / duration); float easedT = ease.Evaluate(t); float current = Mathf.Lerp(from, to, easedT); // Documentation text normalized. SetPosition(current); yield return null; } // Documentation text normalized. SetPosition(to); tweenCo = null; } private void SetPosition(float value) { if (rt != null) { Vector2 pos = rt.anchoredPosition; if (axis == Axis.X) pos.x = value; else pos.y = value; rt.anchoredPosition = pos; } else { Vector3 pos = transform.localPosition; if (axis == Axis.X) pos.x = value; else pos.y = value; transform.localPosition = pos; } } }