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
-
ObjectComponentBehaviourMonoBehaviourGameHud
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.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.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.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.nameObject.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
PanelNearDistance
Slots and road closer than this (canvas units) to the selected tower count double.
public const float PanelNearDistance = 420
Field Value
PanelRoadSpacing
Canvas distance between road samples used for the tower panel placement.
public const float PanelRoadSpacing = 32
Field Value
PanelRoadWeight
Occlusion weight of one road sample (see PanelRoadSpacing) under the tower panel.
public const float PanelRoadWeight = 0.12
Field Value
PanelSlotWeight
Occlusion weight of a build slot (empty or built) under the tower panel.
public const float PanelSlotWeight = 0.75
Field Value
PanelUnitWeight
Occlusion weight of a unit under the tower panel (the unit of the hysteresis threshold).
public const float PanelUnitWeight = 1
Field Value
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
AudioPlayer
The audio player used for interface sounds (found automatically when empty).
public AudioCuePlayer AudioPlayer { get; set; }
Property Value
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
HasSelection
Whether anything is selected or being aimed (what right click and Esc cancel).
public bool HasSelection { get; }
Property Value
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
IsPauseMenuOpen
Whether the pause menu is open.
public bool IsPauseMenuOpen { get; }
Property Value
Runner
The runner this HUD controls.
public SimulationRunner Runner { get; }
Property Value
SelectedBuildSlot
Selected construction slot index (build menu or tower panel), or -1.
public int SelectedBuildSlot { get; }
Property Value
SelectedHero
Selected hero entity id (in HeroSelected), or 0.
public int SelectedHero { get; }
Property Value
SelectedTower
Selected tower entity id (tower panel or rally aiming), or 0.
public int SelectedTower { get; }
Property Value
SelectedUnit
Inspected soldier or enemy entity id (in UnitSelected), or 0.
public int SelectedUnit { get; }
Property Value
ShowPauseState
When false, the top bar never shows the paused state (for captures taken while frozen).
public bool ShowPauseState { get; set; }
Property Value
Texts
The text source of the HUD (the scene's Texts).
public TextResolver Texts { get; }
Property Value
Theme
The theme in use.
public HudTheme Theme { get; }
Property Value
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
centerVector2obstacleVector2clearancefloat
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
worldPointVector2
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
unitsIReadOnlyList<Vector2>slotsIReadOnlyList<Vector2>roadIReadOnlyList<Vector2>towerVector2canvasSizeVector2panelSizeVector2paddingfloattopBarHeightfloatcurrentint
Returns
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
unitsIReadOnlyList<Vector2>towerVector2canvasSizeVector2panelSizeVector2paddingfloattopBarHeightfloatcurrentint
Returns
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
heroIdint
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
sourceSimulationRunnerviewsEntityViewRegistrycameraToUseCamerahudThemeHudThemedebugOverlayTDHud
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
textsTextResolverfailureCommandFailure
Returns
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
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
cornerintunitsIReadOnlyList<Vector2>slotsIReadOnlyList<Vector2>roadIReadOnlyList<Vector2>towerVector2canvasSizeVector2panelSizeVector2paddingfloattopBarHeightfloat
Returns
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
indexint
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
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
slotIndexint
Returns
SelectTower(int)
Opens the tower panel for a tower entity (for tutorials, tools and screenshots).
public void SelectTower(int entityId)
Parameters
entityIdint
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
SetPauseMenu(bool)
Opens or closes the pause menu (pauses or resumes the runner), as the pause button does.
public void SetPauseMenu(bool open)
Parameters
openbool
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
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