Advertisement
NightFox

Unity3d GUI - Ammo bar

May 11th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GuiAmmoBar : MonoBehaviour {
  5.    
  6.     // Variables de control
  7.     public bool flagAmmo = false;
  8.    
  9.     // Variables para la barra de municion
  10.     public Texture2D txAmmoBarBase = null;          // Barra de vida: Base
  11.     public Texture2D txAmmoBarIcon = null;          // Barra de vida: Icono
  12.     public Texture2D[] txAmmoBarLed;                // Barra de vida: Elemento marcador (Array)
  13.     public GUIStyle skAmmo = null;
  14.     public int ammoBarLenght = 0;                   // Numero de elementos de la barra de vida (longitud)
  15.     public int ammoBarSpace = 0;                    // Distancia entre elementos de la barra de vida
  16.     public int ammoTotal = 0;                       // Total de municion
  17.    
  18.    
  19.  
  20.     // Al crearse el objeto...
  21.     void Start () {
  22.        
  23.         // Inicializa la Gui
  24.         InitGui();
  25.    
  26.     }
  27.    
  28.    
  29.    
  30.     // Main GUI
  31.     void OnGUI() {
  32.        
  33.         // Gui de la energia
  34.         if (flagAmmo) DrawGui();
  35.        
  36.     }
  37.    
  38.    
  39.    
  40.     // Inicializaciones
  41.     void InitGui() {
  42.    
  43.         // Variable de uso general
  44.         int i = 0;
  45.        
  46.         // Captura de errores de asignacion
  47.         if (txAmmoBarBase == null) Debug.LogError("<txAmmoBarBase> No asignado.");
  48.         if (txAmmoBarIcon == null) Debug.LogError("<txAmmoBarIcon> No asignado.");
  49.         for (i = 0; i < txAmmoBarLed.Length; i ++) {
  50.             if (txAmmoBarLed[i] == null) Debug.LogError("<txAmmoBarLed["+ i +"]> No asignado.");
  51.         }
  52.        
  53.     }
  54.    
  55.    
  56.    
  57.     // Dibuja la Barra de energia
  58.     void DrawGui() {
  59.        
  60.         // Variables internas
  61.         int i = 0;                                                                      // Indice del bucle
  62.         int tx = 0;                                                                     // ID de textura a mostrar
  63.         int posX = 0;                                                                   // Posicion original del dibujado de la barra (X)
  64.         int fullBlocks = 0;                                                             // nº total de bloques llenos
  65.         float halfBlocks = 0;                                                           // nº de fracciones llenas del bloque a medio completar
  66.         float blockContent = ((float)ammoTotal / (float)ammoBarLenght);                 // nº de fracciones que componen cada bloque lleno
  67.         int lastTx = (txAmmoBarLed.Length - 1);                                         // nº de la ultima textura (ID mas alto)
  68.        
  69.         // Dibuja todos los elementos de la GUI de la barra de energia en un mismo grupo
  70.         GUI.BeginGroup(new Rect(0,(Screen.height - txAmmoBarBase.height), txAmmoBarBase.width, txAmmoBarBase.height));
  71.             // Fondo de la barra
  72.             GUI.DrawTexture(new Rect(0, 0, txAmmoBarBase.width, txAmmoBarBase.height), txAmmoBarBase, ScaleMode.ScaleToFit, true, 0);
  73.             // Icono de municion
  74.             GUI.DrawTexture(new Rect(4, 8, txAmmoBarIcon.width, txAmmoBarIcon.height), txAmmoBarIcon, ScaleMode.ScaleToFit, true, 0);
  75.             // Dibuja los elementos de la barra de municion
  76.             posX = 86;  // Posicion inicial de la barra
  77.             fullBlocks = (int)((ammoBarLenght * Player.ammo) / ammoTotal);  // Calcula cuantos bloques llenos
  78.             for (i = 0; i < ammoBarLenght; i ++) {
  79.                 // Si el player tiene municion...
  80.                 if (Player.ammo > 0) {
  81.                     // Si este segmento esta lleno del todo, usa la textura adecuada
  82.                     if (i < fullBlocks) {
  83.                         tx = lastTx;
  84.                     // Si este segmento esta parcialmente lleno del todo, usa la textura adecuada
  85.                     } else if (i == fullBlocks){
  86.                         halfBlocks = (Player.ammo - (i * blockContent));
  87.                         tx = (int)((halfBlocks * lastTx) / blockContent);
  88.                     // Si este segmento esta vacio del todo, usa la textura adecuada
  89.                     } else {
  90.                         tx = 0;
  91.                     }
  92.                 } else {
  93.                     tx = 0;
  94.                 }
  95.                 // Dibuja todos los elementos de la barra
  96.                 GUI.DrawTexture(new Rect(posX, 24, txAmmoBarLed[tx].width, txAmmoBarLed[tx].height), txAmmoBarLed[tx], ScaleMode.ScaleToFit, true, 0);
  97.                 posX += ammoBarSpace;
  98.             }
  99.             // Marcador digital de la municion
  100.             GUI.Label(new Rect(340, 20, 100, 50), Player.ammo.ToString("000"), skAmmo);
  101.         GUI.EndGroup();
  102.        
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement