Colored HP bar, HP text

This commit is contained in:
henriquenakashima 2020-09-17 23:23:58 -04:00
parent ea5108b0f5
commit 778228bc6b
2 changed files with 106 additions and 5 deletions

View file

@ -10,9 +10,16 @@ public class HeroInfoUI : MonoBehaviour
public Text nameText;
public Image healthBarBgImage;
public Image healthBarFgImage;
public Text hpText;
private int renderedHP = -1;
private int renderedMaxHP = -1;
private Camera mainCamera;
private static Color HIGH_HP_COLOR = new Color(0f, 0.8f, 0f);
private static Color MED_HP_COLOR = new Color(0.8f, 0.8f, 0f);
private static Color LOW_HP_COLOR = new Color(0.8f, 0f, 0f);
public void Init(GameObject sprite, BattleParticipant battleParticipant) {
this.target = sprite;
this.battleParticipant = battleParticipant;
@ -31,8 +38,23 @@ public class HeroInfoUI : MonoBehaviour
Vector3 targetPos = this.mainCamera.WorldToScreenPoint(target.transform.position);
transform.position = targetPos;
int hp = battleParticipant.getHP();
int maxHP = battleParticipant.getMaxHP();
healthBarFgImage.fillAmount = ((float) hp) / maxHP;
int hp = this.battleParticipant.getHP();
int maxHP = this.battleParticipant.getMaxHP();
if (this.renderedHP != hp || this.renderedMaxHP != maxHP) {
this.renderedHP = hp;
this.renderedMaxHP = maxHP;
float percentage = ((float) hp) / maxHP;
this.healthBarFgImage.fillAmount = percentage;
if (percentage >= 0.66) {
this.healthBarFgImage.color = HIGH_HP_COLOR;
} else if (percentage >= 0.33) {
this.healthBarFgImage.color = MED_HP_COLOR;
} else {
this.healthBarFgImage.color = LOW_HP_COLOR;
}
this.hpText.text = "" + hp + " / " + maxHP;
}
}
}

View file

@ -1,5 +1,83 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &5172729878808660606
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8409674319951998954}
- component: {fileID: 6585051337894170405}
- component: {fileID: 2548384344736816241}
m_Layer: 5
m_Name: HPText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8409674319951998954
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5172729878808660606}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6071241564411437881}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 96, y: 0}
m_SizeDelta: {x: 131.56287, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6585051337894170405
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5172729878808660606}
m_CullTransparentMesh: 0
--- !u!114 &2548384344736816241
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5172729878808660606}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.6226415, g: 0.6226415, b: 0.6226415, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 12
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 1
m_MaxSize: 40
m_Alignment: 3
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: 12 / 34
--- !u!1 &6071241563652651137
GameObject:
m_ObjectHideFlags: 0
@ -132,7 +210,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0, g: 1, b: 0.0024728775, a: 1}
m_Color: {r: 0, g: 0.64705884, b: 0, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
@ -255,6 +333,7 @@ RectTransform:
m_Children:
- {fileID: 6071241563652651136}
- {fileID: 6071241564068399932}
- {fileID: 8409674319951998954}
m_Father: {fileID: 6071241564507600362}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -313,7 +392,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9915b94838d288b45b8b73de0aa41d39, type: 3}
m_Name:
m_EditorClassIdentifier:
target: {fileID: 0}
nameText: {fileID: 6071241564363553666}
healthBarBgImage: {fileID: 6071241563652651135}
healthBarFgImage: {fileID: 6071241564068399931}
hpText: {fileID: 2548384344736816241}