HarvesterObjectHealthBar
Introduction
The HarvesterObjectHealthBar
is a C# script component in Unity game engine that handles the health bar of the harvestable objects in the game world. It renders a UI bar that shows the health of the harvestable object, with a configurable maximum health and color scheme.
The component works by subscribing to events emitted by the HarvestableObject
component, which contains information about the current health of the harvestable object. When the health changes, the HarvesterObjectHealthBar
updates the UI bar to reflect the new value.
The health bar can be customized by specifying the maximum health value and the color scheme in the Unity Inspector. Additionally, it can be configured to display a label with the current health value. The UI bar can be anchored to different positions on the screen, and its size can be adjusted.
The HarvesterObjectHealthBar
component is designed to work in conjunction with the HarvestableObject
component, providing players with an easy way to monitor the health of the harvestable objects they are interacting with.
Overview Page
Class Name | HarvesterObjectHealthBar |
Namespace | HNGamers |
Inherits | MonoBehaviour |
Description | A class for creating and managing health bars for harvestable objects in Unity. |
Interface Pages
Interface | MonoBehaviour |
None |
Constructor Pages
Constructor | None |
Method Pages
SetHealthBarData(Transform targetTransform, RectTransform healthBarPanel) | Sets the target transform, health bar panel, and fills in the health bar of the object. |
OnHealthChanged(float healthFill) | Updates the health bar fill amount. |
Property Pages
targetCanvas | A reference to the RectTransform of the target canvas. |
healthBar | A reference to the RectTransform of the health bar. |
objectToFollow | A reference to the Transform of the object to follow. |
Event Pages
None |
Concept/Guide Pages
None |
Examples
Example Usage | “`csharp |
HarvesterObjectHealthBar harvesterObjectHealthBar = GetComponent<HarvesterObjectHealthBar>();
harvesterObjectHealthBar.SetHealthBarData(transform, healthBarPanel);
harvesterObjectHealthBar.OnHealthChanged(healthFill);
``` |
| Example Explanation | This code creates a reference to a HarvesterObjectHealthBar component, sets the target transform and health bar panel, and updates the health bar fill amount. |
Unity Usage
This class is meant to be used within Unity as a way to manage health bars for harvestable objects in a 3D environment. The SetHealthBarData
method should be called to initialize the health bar and the OnHealthChanged
method should be called to update the fill amount of the health bar. The targetCanvas
property should be set to the RectTransform of the canvas that the health bar should be displayed on, the healthBar
property should be set to the RectTransform of the health bar, and the objectToFollow
property should be set to the Transform of the object that the health bar should be attached to.