55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 召唤师专属血条 UI:顶部 HUD 显示双方召唤师血量,区别于单位头顶血条。
|
|
/// </summary>
|
|
public class SummonerHealthBarUI : MonoBehaviour
|
|
{
|
|
[Header("UI 引用")]
|
|
public Image playerHealthFill;
|
|
public Image enemyHealthFill;
|
|
public Text playerHealthText;
|
|
public Text enemyHealthText;
|
|
|
|
[Header("召唤师引用")]
|
|
public GameObject playerSummoner;
|
|
public GameObject enemySummoner;
|
|
|
|
private WorldHealthBar playerHealthBar;
|
|
private WorldHealthBar enemyHealthBar;
|
|
|
|
private void Start()
|
|
{
|
|
// 获取双方召唤师的血条组件
|
|
if (playerSummoner != null)
|
|
{
|
|
playerHealthBar = playerSummoner.GetComponent<WorldHealthBar>();
|
|
}
|
|
|
|
if (enemySummoner != null)
|
|
{
|
|
enemyHealthBar = enemySummoner.GetComponent<WorldHealthBar>();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateHealthBar(playerHealthBar, playerHealthFill, playerHealthText);
|
|
UpdateHealthBar(enemyHealthBar, enemyHealthFill, enemyHealthText);
|
|
}
|
|
|
|
private void UpdateHealthBar(WorldHealthBar healthBar, Image fillImage, Text healthText)
|
|
{
|
|
if (healthBar == null || fillImage == null) return;
|
|
|
|
float ratio = healthBar.currentHealth / healthBar.maxHealth;
|
|
fillImage.fillAmount = ratio;
|
|
|
|
if (healthText != null)
|
|
{
|
|
healthText.text = $"{Mathf.CeilToInt(healthBar.currentHealth)}/{Mathf.CeilToInt(healthBar.maxHealth)}";
|
|
}
|
|
}
|
|
}
|