The HUD
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 58 of 64.
Challenge
MediumBuild a HUD that only listens.
Herogetspublic event Action<int, int> HealthChanged; raise it with(health, maxHealth)inStartand on every change.WaveSpawnergetspublic event Action<int> WaveStarted, raised withWaveas each wave starts.- In
HUD, subscribe inOnEnable, unsubscribe inOnDisable; fillhealthBar, showHP 3/5andWave 2. NoUpdate.
Press Play and fight: the HUD keeps up. Then Check.
Try it yourself
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class HUD : MonoBehaviour
{
[SerializeField] private Hero hero;
[SerializeField] private WaveSpawner spawner;
[SerializeField] private Image healthBar;
[SerializeField] private TMP_Text healthText;
[SerializeField] private TMP_Text waveText;
// A later lesson fills this in: health and wave on screen.
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground