Advertisement
BorisKotlyar

Untitled

Mar 30th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using Animation;
  3. using DG.Tweening;
  4. using UnityEngine;
  5.  
  6. namespace Game.UI.Windows.Elements
  7. {
  8.     /// <summary>
  9.     /// Realization for UI progress View
  10.     /// </summary>
  11.     public class UIProgressBar : MonoBehaviour
  12.     {
  13.         [Header("Body")]
  14.         [SerializeField] private RectTransform _body;
  15.         [SerializeField] private float _defaultMaxWidth;
  16.        
  17.         [Header("Animation")]
  18.         [SerializeField] private DOAnimationSettings _animationSettings;
  19.  
  20.         private Tween _animationTween;
  21.         private Action _onEndAction;
  22.  
  23.         /// <summary>
  24.         /// Set bar value for 0 to 1 value
  25.         /// Use default width
  26.         /// </summary>
  27.         /// <param name="value">current value from 0 to 1</param>
  28.         /// <param name="onEnd">action on end play</param>
  29.         public void SetValue01(float value, Action onEnd = null)
  30.         {
  31.             UpdateSize(Mathf.Clamp01(value) * _defaultMaxWidth, onEnd);
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Set bar value depends on custom max size
  36.         /// </summary>
  37.         /// <param name="value">current size</param>
  38.         /// <param name="onEnd">action on end play</param>
  39.         public void SetValue(float value, Action onEnd = null)
  40.         {
  41.             UpdateSize(value, onEnd);
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Stop animation and reset bar state
  46.         /// </summary>
  47.         public void ResetValues()
  48.         {
  49.             StopAnimation();
  50.  
  51.             // reset size
  52.             var size = _body.sizeDelta;
  53.             size.x = 0f;
  54.             _body.sizeDelta = size;
  55.  
  56.             // clear actions
  57.             _onEndAction = null;
  58.         }
  59.        
  60.         private void StopAnimation()
  61.         {
  62.             // stop and kill animation
  63.             if (_animationTween != null)
  64.             {
  65.                 _animationTween.Kill();
  66.                 _animationTween = null;
  67.             }
  68.         }
  69.  
  70.         private void UpdateSize(float value, Action onEnd = null)
  71.         {
  72.             // cache action
  73.             _onEndAction = onEnd;
  74.  
  75.             // reset animation
  76.             StopAnimation();
  77.  
  78.             // setup and play animation
  79.             _animationTween = _body
  80.                 .DOSizeDelta(new Vector2(value, _body.sizeDelta.y), _animationSettings.Duration)
  81.                 .SetEase(_animationSettings.Ease)
  82.                 .SetDelay(_animationSettings.Delay)
  83.                 .OnComplete(() =>
  84.                 {
  85.                     _onEndAction?.Invoke();
  86.                 });
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement