Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Animation;
- using DG.Tweening;
- using UnityEngine;
- namespace Game.UI.Windows.Elements
- {
- /// <summary>
- /// Realization for UI progress View
- /// </summary>
- public class UIProgressBar : MonoBehaviour
- {
- [Header("Body")]
- [SerializeField] private RectTransform _body;
- [SerializeField] private float _defaultMaxWidth;
- [Header("Animation")]
- [SerializeField] private DOAnimationSettings _animationSettings;
- private Tween _animationTween;
- private Action _onEndAction;
- /// <summary>
- /// Set bar value for 0 to 1 value
- /// Use default width
- /// </summary>
- /// <param name="value">current value from 0 to 1</param>
- /// <param name="onEnd">action on end play</param>
- public void SetValue01(float value, Action onEnd = null)
- {
- UpdateSize(Mathf.Clamp01(value) * _defaultMaxWidth, onEnd);
- }
- /// <summary>
- /// Set bar value depends on custom max size
- /// </summary>
- /// <param name="value">current size</param>
- /// <param name="onEnd">action on end play</param>
- public void SetValue(float value, Action onEnd = null)
- {
- UpdateSize(value, onEnd);
- }
- /// <summary>
- /// Stop animation and reset bar state
- /// </summary>
- public void ResetValues()
- {
- StopAnimation();
- // reset size
- var size = _body.sizeDelta;
- size.x = 0f;
- _body.sizeDelta = size;
- // clear actions
- _onEndAction = null;
- }
- private void StopAnimation()
- {
- // stop and kill animation
- if (_animationTween != null)
- {
- _animationTween.Kill();
- _animationTween = null;
- }
- }
- private void UpdateSize(float value, Action onEnd = null)
- {
- // cache action
- _onEndAction = onEnd;
- // reset animation
- StopAnimation();
- // setup and play animation
- _animationTween = _body
- .DOSizeDelta(new Vector2(value, _body.sizeDelta.y), _animationSettings.Duration)
- .SetEase(_animationSettings.Ease)
- .SetDelay(_animationSettings.Delay)
- .OnComplete(() =>
- {
- _onEndAction?.Invoke();
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement