Class GameHud

Namespace
TDMaker.Unity.Hud
Assembly
TDMaker.Unity.dll

Production HUD for PC (mouse + keyboard), built in uGUI from code and styled by a HudTheme: top bar (gold, lives, wave, pause, speed), wave-call indicators at path entrances (countdown ring, early bonus), radial build menu, tower panel (upgrades, branches, abilities, target priority, rally, sell), hero portraits, spell bar with cooldowns and hotkeys, unit info, pause and victory/defeat screens, tooltips and world feedback (range ellipses, slot highlight, placement ghost, rally/move markers, spell aim). Interaction follows the input grammar of spec 14 (docs/specs/14-input-grammar.md): one selection or aiming state at a time; a battlefield left click acts at the release point; while a hero, rally point or spell is being aimed every battlefield click is a point attempt (valid: act and return to browsing; invalid: feedback and keep aiming); build, upgrade, branch, ability, sell and wave call are single clicks; right click cancels everything (and does nothing without a selection); Esc cancels a selection, otherwise opens/closes the pause menu. Failed or cancelled aims send no command and every result shown is read back from the simulation. Keys: Esc cancel/pause, Space/P pause, 1-9 spells, H hero, N next wave, F1 debug overlay. Text is TextMesh Pro; every string comes from Texts (keys in HudTexts), and the HUD relabels itself when the language or a table changes.

[DefaultExecutionOrder(60)]
[AddComponentMenu("2D Tower Defense Maker/Game HUD")]
public sealed class GameHud : MonoBehaviour
Inheritance
Object
Component
Behaviour
MonoBehaviour
GameHud
Inherited Members
MonoBehaviour.IsInvoking()
MonoBehaviour.CancelInvoke()
MonoBehaviour.StopCoroutine(Coroutine)
MonoBehaviour.StopAllCoroutines()
MonoBehaviour.destroyCancellationToken
MonoBehaviour.useGUILayout
MonoBehaviour.didStart
MonoBehaviour.didAwake
MonoBehaviour.runInEditMode
Behaviour.enabled
Behaviour.isActiveAndEnabled
Component.GetComponent<T>()
Component.TryGetComponent<T>(out T)
Component.GetComponentInChildren<T>()
Component.GetComponentsInChildren<T>()
Component.GetComponentInParent<T>()
Component.GetComponentsInParent<T>()
Component.GetComponents<T>()
Component.GetComponentIndex()
Component.CompareTag(TagHandle)
Component.transform
Component.transformHandle
Component.gameObject
Component.tag
Object.GetEntityId()
Object.GetInstanceID()
Object.GetHashCode()
Object.InstantiateAsync<T>(T)
Object.InstantiateAsync<T>(T, Transform)
Object.InstantiateAsync<T>(T, Vector3, Quaternion)
Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)
Object.Instantiate(Object, Vector3, Quaternion)
Object.Instantiate(Object, Vector3, Quaternion, Transform)
Object.Instantiate(Object)
Object.Instantiate(Object, Scene)
Object.Instantiate<T>(T, InstantiateParameters)
Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)
Object.Instantiate(Object, Transform)
Object.Instantiate<T>(T)
Object.Instantiate<T>(T, Vector3, Quaternion)
Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
Object.Instantiate<T>(T, Transform)
Object.Destroy(Object)
Object.DestroyImmediate(Object)
Object.DontDestroyOnLoad(Object)
Object.DestroyObject(Object)
Object.FindObjectsOfType<T>()
Object.FindObjectsByType<T>(FindObjectsSortMode)
Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)
Object.FindObjectOfType<T>()
Object.FindFirstObjectByType<T>()
Object.FindAnyObjectByType<T>()
Object.FindFirstObjectByType<T>(FindObjectsInactive)
Object.FindAnyObjectByType<T>(FindObjectsInactive)
Object.FindObjectsByType<T>()
Object.FindObjectsByType<T>(FindObjectsInactive)
Object.ToString()
Object.name
Object.hideFlags

Constructors

GameHud()

public GameHud()

Fields

PanelCornerCount

Screen corners the tower panel can sit in: 0 top-left, 1 top-right (both under the top bar), 2 bottom-right.

public const int PanelCornerCount = 3

Field Value

int

PanelNearDistance

Slots and road closer than this (canvas units) to the selected tower count double.

public const float PanelNearDistance = 420

Field Value

float

PanelRoadSpacing

Canvas distance between road samples used for the tower panel placement.

public const float PanelRoadSpacing = 32

Field Value

float

PanelRoadWeight

Occlusion weight of one road sample (see PanelRoadSpacing) under the tower panel.

public const float PanelRoadWeight = 0.12

Field Value

float

PanelSlotWeight

Occlusion weight of a build slot (empty or built) under the tower panel.

public const float PanelSlotWeight = 0.75

Field Value

float

PanelUnitWeight

Occlusion weight of a unit under the tower panel (the unit of the hysteresis threshold).

public const float PanelUnitWeight = 1

Field Value

float

onContinue

Invoked by the result screen's Continue button (hidden when nothing listens in the inspector).

[Header("Result screen")]
public UnityEvent onContinue

Field Value

UnityEvent

onQuit

Invoked by the Quit buttons (hidden when nothing listens in the inspector).

public UnityEvent onQuit

Field Value

UnityEvent

Properties

AimingSpell

Spell slot being aimed, or -1.

public int AimingSpell { get; }

Property Value

int

AudioPlayer

The audio player used for interface sounds (found automatically when empty).

public AudioCuePlayer AudioPlayer { get; set; }

Property Value

AudioCuePlayer

Canvas

The HUD canvas (created on connect).

public Canvas Canvas { get; }

Property Value

Canvas

CurrentMode

Current interaction state.

public GameHud.Mode CurrentMode { get; }

Property Value

GameHud.Mode

HasSelection

Whether anything is selected or being aimed (what right click and Esc cancel).

public bool HasSelection { get; }

Property Value

bool

HeroSkillButtons

Whether hero tiles show skill buttons next to the portrait (off by default: portrait and health only).

public bool HeroSkillButtons { get; set; }

Property Value

bool

IsPauseMenuOpen

Whether the pause menu is open.

public bool IsPauseMenuOpen { get; }

Property Value

bool

Runner

The runner this HUD controls.

public SimulationRunner Runner { get; }

Property Value

SimulationRunner

SelectedBuildSlot

Selected construction slot index (build menu or tower panel), or -1.

public int SelectedBuildSlot { get; }

Property Value

int

SelectedHero

Selected hero entity id (in HeroSelected), or 0.

public int SelectedHero { get; }

Property Value

int

SelectedTower

Selected tower entity id (tower panel or rally aiming), or 0.

public int SelectedTower { get; }

Property Value

int

SelectedUnit

Inspected soldier or enemy entity id (in UnitSelected), or 0.

public int SelectedUnit { get; }

Property Value

int

ShowPauseState

When false, the top bar never shows the paused state (for captures taken while frozen).

public bool ShowPauseState { get; set; }

Property Value

bool

Texts

The text source of the HUD (the scene's Texts).

public TextResolver Texts { get; }

Property Value

TextResolver

Theme

The theme in use.

public HudTheme Theme { get; }

Property Value

HudTheme

Methods

AvoidPoint(Vector2, Vector2, float)

Moves center so it is at least clearance away from obstacle (straight away from it; straight up when they coincide). Unchanged when already clear.

public static Vector2 AvoidPoint(Vector2 center, Vector2 obstacle, float clearance)

Parameters

center Vector2
obstacle Vector2
clearance float

Returns

Vector2

BattlefieldClick(Vector2)

A left click that reached the battlefield at worldPoint (no UI under it). While a spell, rally point or hero move is being aimed this is a point attempt at that position (whatever is under it); otherwise it selects the top target under the point, or clears the selection on empty ground.

public void BattlefieldClick(Vector2 worldPoint)

Parameters

worldPoint Vector2

CancelAll()

Cancels every selection and aim (right click, Esc with a selection).

public void CancelAll()

ChoosePanelCorner(IReadOnlyList<Vector2>, IReadOnlyList<Vector2>, IReadOnlyList<Vector2>, Vector2, Vector2, Vector2, float, float, int)

Like the overload above, but also scores the map the panel would hide: build slots (slots) and road samples (road, see PanelRoadSpacing), counted double near the selected tower. Anything else (decoration) is free, so when every corner covers something the least covered one wins. Hysteresis as above.

public static int ChoosePanelCorner(IReadOnlyList<Vector2> units, IReadOnlyList<Vector2> slots, IReadOnlyList<Vector2> road, Vector2 tower, Vector2 canvasSize, Vector2 panelSize, float padding, float topBarHeight, int current = -1)

Parameters

units IReadOnlyList<Vector2>
slots IReadOnlyList<Vector2>
road IReadOnlyList<Vector2>
tower Vector2
canvasSize Vector2
panelSize Vector2
padding float
topBarHeight float
current int

Returns

int

ChoosePanelCorner(IReadOnlyList<Vector2>, Vector2, Vector2, Vector2, float, float, int)

The corner for the tower panel that covers the least of the fight: never the selected tower (tower), then as few units (units, canvas positions) as possible; ties go to the side away from the tower, then to the top. current (or -1) is kept unless another corner covers at least two units fewer, so the panel does not jump around while the fight moves.

public static int ChoosePanelCorner(IReadOnlyList<Vector2> units, Vector2 tower, Vector2 canvasSize, Vector2 panelSize, float padding, float topBarHeight, int current = -1)

Parameters

units IReadOnlyList<Vector2>
tower Vector2
canvasSize Vector2
panelSize Vector2
padding float
topBarHeight float
current int

Returns

int

ClearNavigation()

Returns to the default rule (buttons show when the inspector events have listeners).

public void ClearNavigation()

ClearSelection()

Closes menus, selection and previews (a spell being aimed is kept; CancelAll() clears it too).

public void ClearSelection()

ClickHeroPortrait(int)

A click on a hero portrait: selects that hero, or deselects it when it is already selected.

public void ClickHeroPortrait(int heroId)

Parameters

heroId int

Configure(SimulationRunner, EntityViewRegistry, Camera, HudTheme, TDHud)

Assigns dependencies; call before enabling.

public void Configure(SimulationRunner source, EntityViewRegistry views, Camera cameraToUse, HudTheme hudTheme, TDHud debugOverlay = null)

Parameters

source SimulationRunner
views EntityViewRegistry
cameraToUse Camera
hudTheme HudTheme
debugOverlay TDHud

Connect()

Builds the canvas (once) and subscribes to the runner. Called from OnEnable; tools may call it in edit mode.

public void Connect()

Describe(TextResolver, CommandFailure)

The player-facing text of a rejected command (keys hud.reject.*).

public static string Describe(TextResolver texts, CommandFailure failure)

Parameters

texts TextResolver
failure CommandFailure

Returns

string

EnsureEventSystem()

Creates an UnityEngine.EventSystems.EventSystem with the input module matching the active input backend when the scene has none.

public static void EnsureEventSystem()

PanelCornerPosition(int, Vector2, Vector2, float, float)

Top-left position (canvas units, centre origin) of the tower panel in corner.

public static Vector2 PanelCornerPosition(int corner, Vector2 canvasSize, Vector2 panelSize, float padding, float topBarHeight)

Parameters

corner int
canvasSize Vector2
panelSize Vector2
padding float
topBarHeight float

Returns

Vector2

PanelCornerScore(int, IReadOnlyList<Vector2>, IReadOnlyList<Vector2>, IReadOnlyList<Vector2>, Vector2, Vector2, Vector2, float, float)

How much of the fight and the map the tower panel hides in corner (lower is better; 1000+ covers the tower).

public static float PanelCornerScore(int corner, IReadOnlyList<Vector2> units, IReadOnlyList<Vector2> slots, IReadOnlyList<Vector2> road, Vector2 tower, Vector2 canvasSize, Vector2 panelSize, float padding, float topBarHeight)

Parameters

corner int
units IReadOnlyList<Vector2>
slots IReadOnlyList<Vector2>
road IReadOnlyList<Vector2>
tower Vector2
canvasSize Vector2
panelSize Vector2
padding float
topBarHeight float

Returns

float

PressEscape()

Esc: closes the pause menu when it is open; otherwise cancels the selection or aim when there is one; otherwise opens the pause menu. Only the first matching branch runs.

public void PressEscape()

PressHeroKey()

The hero key: selects the first hero; with a hero selected, the next one; after the last one it deselects (with a single hero the key toggles its selection).

public void PressHeroKey()

PressSpellKey(int)

A spell hotkey (0-based slot): selects a ready spell for aiming, cancels it when it is already aimed, otherwise does nothing.

public void PressSpellKey(int index)

Parameters

index int

Refresh()

Updates every widget from the simulation state (no input). Called each frame; tools may call it directly.

public void Refresh()

SampleSegment(Vector2, Vector2, float, List<Vector2>)

Appends points every spacing (or closer) from a towards b (b excluded).

public static void SampleSegment(Vector2 a, Vector2 b, float spacing, List<Vector2> into)

Parameters

a Vector2
b Vector2
spacing float
into List<Vector2>

SecondaryClick()

A right click: cancels every selection and aim (menus, info, rally, spell, hero). Does nothing without a selection.

public void SecondaryClick()

SelectBuildSlot(int)

Selects a construction slot as a click on it would: opens the build menu on an empty slot or the tower panel on an occupied one (for tutorials, tools, keyboard or gamepad navigation and tests). Returns false when no simulation is running or the index is out of range.

public bool SelectBuildSlot(int slotIndex)

Parameters

slotIndex int

Returns

bool

SelectTower(int)

Opens the tower panel for a tower entity (for tutorials, tools and screenshots).

public void SelectTower(int entityId)

Parameters

entityId int

SetNavigation(bool, bool, string, string)

For flow scripts that add onContinue/onQuit listeners from code (runtime listeners are not visible to the default "show when the inspector has a listener" rule): chooses which navigation buttons the result screen and pause menu show, and their labels. With both shown, the result screen puts Quit and Continue side by side. The labels are text keys (looked up in Texts) or literal texts; empty uses NavContinue / NavQuit.

public void SetNavigation(bool showContinue, bool showQuit, string continueText = "hud.nav.continue", string quitText = "hud.nav.quit")

Parameters

showContinue bool
showQuit bool
continueText string
quitText string

SetPauseMenu(bool)

Opens or closes the pause menu (pauses or resumes the runner), as the pause button does.

public void SetPauseMenu(bool open)

Parameters

open bool

ShowFirstBattleTip()

Shows the first battle's build/upgrade hint long enough to read before calling the wave.

public void ShowFirstBattleTip()

Toast(string, bool)

Shows a short message above the spell bar.

public void Toast(string message, bool warning)

Parameters

message string
warning bool

Events

WaveCalledEarly

Raised when a wave was called early, with the bonus gold it paid (the HUD itself shows no message for it).

public event Action<int> WaveCalledEarly

Event Type

Action<int>