Last active
January 15, 2021 08:18
-
-
Save Hellhackee/08fe366b1e05421734ecc1c2c3e9bbca to your computer and use it in GitHub Desktop.
Lesson UI (script UI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using DG.Tweening; | |
using UnityEngine.Events; | |
public class UI : MonoBehaviour | |
{ | |
[SerializeField] private Button _attackButton; | |
[SerializeField] private Button _healButton; | |
[SerializeField] private Slider _healthSlider; | |
[SerializeField] private Text _currentHealthLabel; | |
[SerializeField] private Text _maxHealthLabel; | |
[SerializeField] private Player _player; | |
private int _damage; | |
private int _healAmount; | |
private void Start() | |
{ | |
_damage = 10; | |
_healAmount = 10; | |
} | |
private void OnEnable() | |
{ | |
_player.HealthChanged += OnHealthChanged; | |
_attackButton.onClick.AddListener(OnAttackButtonPressed); | |
_healButton.onClick.AddListener(OnHealButtonPressed); | |
} | |
private void OnDisable() | |
{ | |
_attackButton.onClick.RemoveListener(OnAttackButtonPressed); | |
_healButton.onClick.RemoveListener(OnHealButtonPressed); | |
} | |
private void OnAttackButtonPressed() | |
{ | |
_player.ChangeHealth(-_damage); | |
} | |
private void OnHealButtonPressed() | |
{ | |
_player.ChangeHealth(_healAmount); | |
} | |
private void OnHealthChanged(int value, int maxValue) | |
{ | |
float sliderValue = (float)value / maxValue; | |
_healthSlider.DOValue(sliderValue, 1); | |
_maxHealthLabel.text = "Максимальное здоровье: " + maxValue.ToString(); | |
_currentHealthLabel.text = "Текущее здоровье: " + value.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment