LegitTeddyBears

HealthUI

Jun 3rd, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class HealthUI : MonoBehaviour {
  7.     public Slider slider;
  8.     // Use this for initialization
  9.     void Start ()
  10.     {
  11.         MessageHandler msgHandler = GetComponent<MessageHandler>();
  12.         if (msgHandler)
  13.         {
  14.             msgHandler.RegisterDelegate(RecieveMessage);
  15.         }
  16.     }
  17.     void RecieveMessage(MessageType msgType, GameObject go, MessageData msgData)
  18.     {
  19.         switch (msgType)
  20.         {
  21.             case MessageType.HEALTHCHANGED:
  22.                 HealthData hpData = msgData as HealthData;
  23.                 if (hpData != null)
  24.                 {
  25.                     UpdateUI(hpData.maxHealth, hpData.curHealth);
  26.                 }
  27.                 break;
  28.         }
  29.     }
  30.    
  31.     void UpdateUI(int maxHealth, int curHealth)
  32.     {
  33.         slider.value = (1.0f / maxHealth) * curHealth;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment