Advertisement
centner_dc

UI_Slider_Healthbar

Sep 29th, 2020
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using Sirenix.OdinInspector;
  2. using UnityEngine;
  3. using Tools;
  4. using Player;
  5. using TMPro;
  6.  
  7. namespace UI
  8. {
  9.     public class Healthbar : SerializedMonoBehaviour
  10.     {
  11.         [SerializeField] private TMP_Text _healthText;
  12.         [SerializeField] private ISliderAnimation _sliderAnimation;
  13.         [SerializeField] private IHealthy _healths;
  14.  
  15.         private void OnValidate()
  16.         {
  17.             _sliderAnimation.VerifyNotNull<ISliderAnimation>(nameof(_sliderAnimation));
  18.             _healths.VerifyNotNull<IHealthy>(nameof(_healths));
  19.         }
  20.  
  21.         private void OnEnable()
  22.         {
  23.             _healths.HealthChanged += OnHealthChange;
  24.         }
  25.  
  26.         private void OnDisable()
  27.         {
  28.             _healths.HealthChanged -= OnHealthChange;
  29.         }
  30.  
  31.         private void Start()
  32.         {
  33.             OnHealthChange(_healths.Amount);
  34.         }
  35.  
  36.         private void OnHealthChange(float health)
  37.         {
  38.             _sliderAnimation.SetValue(health / _healths.MaxAmount);
  39.             _healthText.text = health.ToString("F0");
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement