You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using System;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Unity.BossRoom.Gameplay.UI
|
|
{
|
|
/// <summary>
|
|
/// UI object that visually represents an object's health. Visuals are updated when NetworkVariable is modified.
|
|
/// </summary>
|
|
public class UIHealth : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
Slider m_HitPointsSlider;
|
|
|
|
NetworkVariable<int> m_NetworkedHealth;
|
|
|
|
public void Initialize(NetworkVariable<int> networkedHealth, int maxValue)
|
|
{
|
|
m_NetworkedHealth = networkedHealth;
|
|
|
|
m_HitPointsSlider.minValue = 0;
|
|
m_HitPointsSlider.maxValue = maxValue;
|
|
HealthChanged(maxValue, maxValue);
|
|
|
|
m_NetworkedHealth.OnValueChanged += HealthChanged;
|
|
}
|
|
|
|
void HealthChanged(int previousValue, int newValue)
|
|
{
|
|
m_HitPointsSlider.value = newValue;
|
|
// disable slider when we're at full health!
|
|
m_HitPointsSlider.gameObject.SetActive(m_HitPointsSlider.value != m_HitPointsSlider.maxValue);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
m_NetworkedHealth.OnValueChanged -= HealthChanged;
|
|
}
|
|
}
|
|
}
|