Files
duckovMod/history/HealthBarPatch.cs
2025-10-21 20:09:31 +08:00

107 lines
3.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HarmonyLib;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace test
{
/// <summary>
/// Harmony补丁类用于修改HealthBar的行为
/// 添加血量数值显示功能
/// </summary>
[HarmonyPatch]
public class HealthBarPatch
{
// 使用字典来存储每个HealthBar实例对应的血量文本
private static readonly Dictionary<Duckov.UI.HealthBar, TextMeshProUGUI> healthTexts = new Dictionary<Duckov.UI.HealthBar, TextMeshProUGUI>();
/// <summary>
/// 补丁HealthBar的Setup方法初始化血量文本
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(Duckov.UI.HealthBar), "Setup")]
static void SetupPostfix(Duckov.UI.HealthBar __instance)
{
// 如果这个HealthBar已经有对应的文本先清理
if (healthTexts.ContainsKey(__instance))
{
CleanupHealthText(__instance);
}
// 创建血量文本对象
GameObject textObj = new GameObject("HealthText");
textObj.transform.SetParent(__instance.transform);
// 设置文本组件
TextMeshProUGUI healthText = textObj.AddComponent<TextMeshProUGUI>();
healthText.fontSize = 12;
healthText.color = Color.white;
healthText.alignment = TextAlignmentOptions.Center;
// 设置文本位置
RectTransform rectTransform = textObj.GetComponent<RectTransform>();
rectTransform.anchorMin = new Vector2(0, 0);
rectTransform.anchorMax = new Vector2(1, 1);
rectTransform.offsetMin = new Vector2(0, 0);
rectTransform.offsetMax = new Vector2(0, 0);
// 将文本对象存储到字典中
healthTexts[__instance] = healthText;
// 更新血量文本
UpdateHealthText(__instance);
}
/// <summary>
/// 补丁HealthBar的Refresh方法更新血量文本
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(Duckov.UI.HealthBar), "Refresh")]
static void RefreshPostfix(Duckov.UI.HealthBar __instance)
{
UpdateHealthText(__instance);
}
/// <summary>
/// 补丁HealthBar的Release方法清理血量文本
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(Duckov.UI.HealthBar), "Release")]
static void ReleasePostfix(Duckov.UI.HealthBar __instance)
{
CleanupHealthText(__instance);
}
/// <summary>
/// 清理与HealthBar关联的血量文本
/// </summary>
static void CleanupHealthText(Duckov.UI.HealthBar healthBar)
{
if (healthTexts.TryGetValue(healthBar, out TextMeshProUGUI textComponent))
{
if (textComponent != null && textComponent.gameObject != null)
{
Object.DestroyImmediate(textComponent.gameObject);
}
healthTexts.Remove(healthBar);
}
}
/// <summary>
/// 更新血量文本显示
/// </summary>
static void UpdateHealthText(Duckov.UI.HealthBar healthBar)
{
if (!healthTexts.TryGetValue(healthBar, out TextMeshProUGUI textComponent) ||
textComponent == null ||
healthBar.target == null)
return;
float currentHealth = healthBar.target.CurrentHealth;
float maxHealth = healthBar.target.MaxHealth;
// 显示当前血量和最大血量
textComponent.text = $"{currentHealth:F0}/{maxHealth:F0}";
}
}
}