Advertisement
SvOzMaS

FullScreenHealthGUI

Nov 12th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class FullScreenHealthGUI : MonoBehaviour {
  5.  
  6.     public Texture2D[] screenBloodTextures;                     //container array of the blood screen textures
  7.     public Image hitImage;
  8.     public float flashSpeed = 5f;                               //The speed the damageImage will fade at.
  9.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     //The colour the damageImage is set to, to flash.
  10.     public int drawDepth;
  11.     private float alpha;                                          //Transparency to apply to screen textures  
  12.  
  13.     public CharacterHealth characterHealth;
  14.  
  15.  
  16.     private void hurtFlashing() {
  17.         // If the player has just been damaged...
  18.         if (characterHealth.isHurting && !characterHealth.isDead) {
  19.             // ... set the colour of the damageImage to the flash colour.
  20.             hitImage.color = flashColour;
  21.         }
  22.         // Otherwise...
  23.         else {
  24.             // ... transition the colour back to clear.
  25.             hitImage.color = Color.Lerp(hitImage.color, Color.clear, flashSpeed * Time.deltaTime);
  26.         }
  27.     }
  28.  
  29.     void OnGUI() {
  30.  
  31.         if (characterHealth.isDead) {
  32.             return;
  33.         }
  34.  
  35.         hurtFlashing();
  36.  
  37.         //get the corresponding texture array index
  38.         float percentagePerImage = 100 / screenBloodTextures.Length;
  39.         float takenHealthPercentage = 100 - ((characterHealth.currentHealth * 100) / characterHealth.maxHealth);
  40.         int textureIndex = (int)(takenHealthPercentage / percentagePerImage);
  41.  
  42.         if (textureIndex == screenBloodTextures.Length)
  43.             textureIndex--;
  44.  
  45.         //adjusting alpha based on player's left health percetage
  46.         alpha = (1 - (characterHealth.currentHealth * 100 / characterHealth.maxHealth) / 100);
  47.  
  48.         //drawing the texture
  49.         GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
  50.         GUI.depth = drawDepth;
  51.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), screenBloodTextures[textureIndex]);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement