Skip to content

Instantly share code, notes, and snippets.

@Hellhackee
Last active January 15, 2021 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hellhackee/08fe366b1e05421734ecc1c2c3e9bbca to your computer and use it in GitHub Desktop.
Save Hellhackee/08fe366b1e05421734ecc1c2c3e9bbca to your computer and use it in GitHub Desktop.
Lesson UI (script UI)
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