https://github.com/Dartteon/UnityHealthBar
This is a starter kit for a health bar system in Unity.
https://github.com/Dartteon/UnityHealthBar
unity unity-asset
Last synced: about 1 year ago
JSON representation
This is a starter kit for a health bar system in Unity.
- Host: GitHub
- URL: https://github.com/Dartteon/UnityHealthBar
- Owner: Dartteon
- Created: 2017-02-24T19:00:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-24T20:22:30.000Z (over 9 years ago)
- Last Synced: 2024-08-02T05:22:29.710Z (almost 2 years ago)
- Topics: unity, unity-asset
- Size: 18.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UnityHealthBar
This is a starter kit for a health bar system in Unity.
![alt text][sampleImage]
[sampleImage]: https://github.com/Dartteon/UnityHealthBar/blob/master/GithubImage.PNG "Sample"
##Importing to Your Project
Simply open your Unity project, and then double-click the package file (HealthBarStarterPack.unitypackage).
A popup will appear in Unity to prompt you - click "Import", and all the needed files will be inside your project.
##Example
To view the example, drag the TestEnemy (HealthBarStarterPack/Prefabs/TestEnemy) prefab into your scene.
When you press Play, a HealthBar should spawn above it. When you hold SpaceBar down, it should get damaged gradually, and the health bar should reflect that!
##Using the Health System
The Health class is lightweight and simple to use. To use it, attach it to the GameObject you want it on.
The Health system must be initialized with ```Initialize(float maxHealthAmount)```.
To deal damage, call ```Damage (float amount)```.
##Using the BarUI System with Health System
1) Drag and drop the HealthBar prefab into your scene
2) Attach the HealthBar gameObject from (1) onto the gameObject you want (i.e Player)
3) Attach the Health script onto the same gameObject from (2)
4) Inside the master script of the gameObject (e.g Player), add this chunk to ```Start()``` (or Initialize() if you made your own)
(Please change the value inside `Initialize(100f)` to whatever Health amount you want)
```
health = transform.GetComponent ();
health.Initialize (100f);
BarUI barUi = GameObject.Instantiate(healthBarPrefab, transform).GetComponent ();
barUi.transform.localPosition = new Vector3 (0f, .4f, 0f);
barUi.Initialize (health);
```
###Using BarUI on Other Things
If you wish to use this on other systems (e.g Mana Systems), follow these steps:
1) Ensure your new class inherits from `ObservableRatio` class (i.e `public class ManaSystem : ObservableRatio`)
2) Override `GetRatio()` by making a new method in the class,
i.e `public override float GetRatio() { //calculate ratio here }`
3) Drag HealthBar to your gameObject, and rename it accordingly (i.e ManaSystem)
4) Ensure that the BarUI in HealthBar gets initialized to observe your class. Here is an example, for a class `ManaSystem`, with the HealthBar gameObject renamed to ManaBar
```
ManaSystem manaSystem = transform.GetComponent ();
manaSystem.Initialize (100f);
BarUI barUi = transform.Find("ManaBar").GetComponent ();
barUi.Initialize (manaSystem);
```