这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

95 行
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UIHealthBarManager : MonoBehaviour
{
[SerializeField]
private HealthSO _currentHealth = default;
[SerializeField]
private HealthConfigSO _healthConfig = default;
[SerializeField] private UIHeartDisplay[] _heartImages = default;
[SerializeField] private TextMeshProUGUI healthText = default;
[Header("Listening to")]
[SerializeField] private VoidEventChannelSO _deathEvent = default;
[SerializeField] private VoidEventChannelSO _updateHealthEvent = default;
private void OnEnable()
{
_deathEvent.OnEventRaised += SetHealthBar;
_updateHealthEvent.OnEventRaised += SetHeartImages;
_deathEvent.OnEventRaised += RegisterDeath;
SetHealthBar();
}
private void OnDestroy()
{
_updateHealthEvent.OnEventRaised -= SetHeartImages;
_deathEvent.OnEventRaised -= SetHealthBar;
_deathEvent.OnEventRaised -= RegisterDeath;
}
private void Start()
{
}
private void OnLevelWasLoaded(int level)
{
SetHeartImages();
}
public void SetHealthBar()
{
_currentHealth.SetMaxHealth(_healthConfig.MaxHealth);
_currentHealth.SetCurrentHealth(_healthConfig.MaxHealth);
SetHeartImages();
}
public void InflictDamage(int _damage)
{
SetHeartImages();
}
public void RestoreHealth(int _healthToAdd)
{
SetHeartImages();
}
public void RegisterDeath()
{
SetHealthBar();
}
void SetHeartImages()
{
int heartValue = _currentHealth.MaxHealth / _heartImages.Length;
int filledHeartCount = Mathf.FloorToInt((float)_currentHealth.CurrentHealth / heartValue);
for (int i = 0; i < _heartImages.Length; i++)
{
float heartPercent = 0;
if (i < filledHeartCount)
{
heartPercent = 1;
}
else if (i == filledHeartCount)
{
heartPercent = ((float)_currentHealth.CurrentHealth - (float)filledHeartCount * (float)heartValue) / (float)heartValue;
}
else
{
heartPercent = 0;
}
_heartImages[i].SetImage(heartPercent);
}
}
}