2y ago · Sep 28, 2023 4:45 PM
Here is a complete helper for drawing centered player names above the player head and a dynamic colored health bar alongside the bounding box in Unity:
CSHARP
using UnityEngine;
public static class NameHealthESP
{
private static GUIStyle _nameStyle;
public static GUIStyle NameStyle
{
get
{
if (_nameStyle == null)
{
_nameStyle = new GUIStyle(GUI.skin.label)
{
alignment = TextAnchor.MiddleCenter,
fontSize = 12,
fontStyle = FontStyle.Bold
};
_nameStyle.normal.textColor = Color.white;
}
return _nameStyle;
}
}
public static void DrawPlayerDetails(float boxX, float boxY, float boxWidth, float boxHeight, string name, float currentHealth, float maxHealth)
{
// 1. Draw Player Name above Box
GUIContent content = new GUIContent(name);
Vector2 size = NameStyle.CalcSize(content);
float nameX = boxX + (boxWidth * 0.5f) - (size.x * 0.5f);
float nameY = boxY - size.y - 3f;
// Black drop shadow
GUI.color = Color.black;
GUI.Label(new Rect(nameX + 1, nameY + 1, size.x, size.y), content, NameStyle);
GUI.color = Color.white;
GUI.Label(new Rect(nameX, nameY, size.x, size.y), content, NameStyle);
// 2. Draw Health Bar on Left Side
float barWidth = 4f;
float barX = boxX - barWidth - 3f;
float healthPercent = Mathf.Clamp01(currentHealth / maxHealth);
float healthHeight = boxHeight * healthPercent;
float healthY = boxY + (boxHeight - healthHeight);
// Background (Black)
GUI.color = new Color(0f, 0f, 0f, 0.7f);
GUI.DrawTexture(new Rect(barX, boxY, barWidth, boxHeight), Texture2D.whiteTexture);
// Foreground Health Color (Green -> Yellow -> Red)
Color healthColor = Color.Lerp(Color.red, Color.green, healthPercent);
GUI.color = healthColor;
GUI.DrawTexture(new Rect(barX, healthY, barWidth, healthHeight), Texture2D.whiteTexture);
}
}
ParanormalDev | MelonLoader & ImGui Modding
The following users thanked ParanormalDev for this post: