您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

40 行
1.2 KiB

using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
namespace Unity.Multiplayer.Samples.BossRoom
{
/// <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;
}
}
}