PlayMode and unit testing a player health bar with events, logging, and exceptions
In this recipe, we will combine many different kinds of tests for a feature that’s included in many games – a visual health bar representing the player’s numeric health value (in this case, a float number from 0.0 to 1.0). Although it doesn’t comprehensively test all aspects of the health bar, this recipe will provide a good example of how we can go about testing many different parts of a game using the Unity testing tools.
A Unity package has been provided that contains the following:
Player.cs
: A player script class for managing values for player health that uses delegates and events to publish health changes to any listeningView
classes.- Two
View
classes that register to listen for player health change events:HealthBarDisplay.cs
: This updatesfillAmount
for a UI image for each new player health value that’s received.HealthChangeLogger...