Advertisement
centner_dc

UI_Slider_HealthbarAnimation

Sep 29th, 2020 (edited)
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using DG.Tweening;
  4. using Tools;
  5.  
  6. namespace UI
  7. {
  8.     [RequireComponent(typeof(Slider))]
  9.     public class HealthbarAnimation : MonoBehaviour, ISliderAnimation
  10.     {
  11.         [SerializeField] private Image _fillImage;
  12.         [SerializeField] private int _blinkRate = 2;
  13.         [SerializeField] private float _speedIncrease = 0.5f;
  14.         [SerializeField] private float _speedDecrease = 3f;
  15.  
  16.         private Slider _slider;
  17.         private Tweener _sliderValueTweener;
  18.         private Color _emptyColor, _fullColor;
  19.         private float _shakeDuration = 1f, _shakeStrength = 10f;
  20.  
  21.         private void OnValidate()
  22.         {
  23.             _fillImage.VerifyNotNull<Image>(nameof(_fillImage));
  24.         }
  25.  
  26.         private void Awake()
  27.         {
  28.             _slider = GetComponent<Slider>();
  29.             _emptyColor = Color.red;
  30.             _fullColor = Color.green;
  31.         }
  32.  
  33.         private void OnEnable()
  34.         {
  35.             SetFillHue(_slider.value);
  36.         }
  37.  
  38.         private float GetFillHue(float value)
  39.         {
  40.             return Mathf.Lerp(_emptyColor.GetHue(), _fullColor.GetHue(), value);
  41.         }
  42.  
  43.         private void SetFillHue(float value)
  44.         {
  45.             var hue = GetFillHue(value);
  46.             _fillImage.color = ColorExtension.SetHue(_emptyColor, hue);
  47.         }
  48.  
  49.         public void SetValue(float value)
  50.         {
  51.             value = Mathf.Clamp(value, 0f, 1f);
  52.  
  53.             var isDecrease = value < _slider.value || value == 0;
  54.             if (isDecrease)
  55.             {
  56.                 DecreaseAnimation(value);
  57.             }
  58.             else
  59.             {
  60.                 IncreaseAnimation(value);
  61.             }
  62.         }
  63.  
  64.         private void DecreaseAnimation(float value)
  65.         {
  66.             ShakeAnimation();
  67.  
  68.             var duration = _slider.maxValue / _speedDecrease;
  69.  
  70.             BlinkAnimation(duration);
  71.  
  72.             ChangeValueAnimation(value, duration);
  73.         }
  74.  
  75.         private void IncreaseAnimation(float value)
  76.         {
  77.             var duration = _slider.maxValue / _speedIncrease;
  78.  
  79.             SetColorAnimation(value, duration);
  80.  
  81.             ChangeValueAnimation(value, duration);
  82.         }
  83.  
  84.         private void ChangeValueAnimation(float value, float duration)
  85.         {
  86.             _sliderValueTweener?.Kill();
  87.  
  88.             _sliderValueTweener = _slider.DOValue(value, duration);
  89.         }
  90.  
  91.         private void ShakeAnimation() =>
  92.             transform.DOShakePosition(_shakeDuration, _shakeStrength);
  93.  
  94.         private void SetColorAnimation(float value, float duration)
  95.         {
  96.             DOTween
  97.                 .To(
  98.                     () => GetFillHue(_slider.value),
  99.                     x => _fillImage.color = ColorExtension.SetHue(_fillImage.color, x),
  100.                     GetFillHue(value),
  101.                     duration);
  102.         }
  103.  
  104.         private void BlinkAnimation(float duration)
  105.         {
  106.             var loops = _blinkRate * 2;
  107.             var oneLoopDuration = duration / loops;
  108.  
  109.             DOTween
  110.                 .To(
  111.                     () => GetFillHue(_slider.value),
  112.                     x => _fillImage.color = ColorExtension.SetHue(_fillImage.color, x),
  113.                     _emptyColor.GetHue(),
  114.                     oneLoopDuration)
  115.                 .SetLoops(loops, LoopType.Yoyo).SetEase(Ease.Linear);
  116.         }
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement