Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class GuiAmmoBar : MonoBehaviour {
- // Variables de control
- public bool flagAmmo = false;
- // Variables para la barra de municion
- public Texture2D txAmmoBarBase = null; // Barra de vida: Base
- public Texture2D txAmmoBarIcon = null; // Barra de vida: Icono
- public Texture2D[] txAmmoBarLed; // Barra de vida: Elemento marcador (Array)
- public GUIStyle skAmmo = null;
- public int ammoBarLenght = 0; // Numero de elementos de la barra de vida (longitud)
- public int ammoBarSpace = 0; // Distancia entre elementos de la barra de vida
- public int ammoTotal = 0; // Total de municion
- // Al crearse el objeto...
- void Start () {
- // Inicializa la Gui
- InitGui();
- }
- // Main GUI
- void OnGUI() {
- // Gui de la energia
- if (flagAmmo) DrawGui();
- }
- // Inicializaciones
- void InitGui() {
- // Variable de uso general
- int i = 0;
- // Captura de errores de asignacion
- if (txAmmoBarBase == null) Debug.LogError("<txAmmoBarBase> No asignado.");
- if (txAmmoBarIcon == null) Debug.LogError("<txAmmoBarIcon> No asignado.");
- for (i = 0; i < txAmmoBarLed.Length; i ++) {
- if (txAmmoBarLed[i] == null) Debug.LogError("<txAmmoBarLed["+ i +"]> No asignado.");
- }
- }
- // Dibuja la Barra de energia
- void DrawGui() {
- // Variables internas
- int i = 0; // Indice del bucle
- int tx = 0; // ID de textura a mostrar
- int posX = 0; // Posicion original del dibujado de la barra (X)
- int fullBlocks = 0; // nº total de bloques llenos
- float halfBlocks = 0; // nº de fracciones llenas del bloque a medio completar
- float blockContent = ((float)ammoTotal / (float)ammoBarLenght); // nº de fracciones que componen cada bloque lleno
- int lastTx = (txAmmoBarLed.Length - 1); // nº de la ultima textura (ID mas alto)
- // Dibuja todos los elementos de la GUI de la barra de energia en un mismo grupo
- GUI.BeginGroup(new Rect(0,(Screen.height - txAmmoBarBase.height), txAmmoBarBase.width, txAmmoBarBase.height));
- // Fondo de la barra
- GUI.DrawTexture(new Rect(0, 0, txAmmoBarBase.width, txAmmoBarBase.height), txAmmoBarBase, ScaleMode.ScaleToFit, true, 0);
- // Icono de municion
- GUI.DrawTexture(new Rect(4, 8, txAmmoBarIcon.width, txAmmoBarIcon.height), txAmmoBarIcon, ScaleMode.ScaleToFit, true, 0);
- // Dibuja los elementos de la barra de municion
- posX = 86; // Posicion inicial de la barra
- fullBlocks = (int)((ammoBarLenght * Player.ammo) / ammoTotal); // Calcula cuantos bloques llenos
- for (i = 0; i < ammoBarLenght; i ++) {
- // Si el player tiene municion...
- if (Player.ammo > 0) {
- // Si este segmento esta lleno del todo, usa la textura adecuada
- if (i < fullBlocks) {
- tx = lastTx;
- // Si este segmento esta parcialmente lleno del todo, usa la textura adecuada
- } else if (i == fullBlocks){
- halfBlocks = (Player.ammo - (i * blockContent));
- tx = (int)((halfBlocks * lastTx) / blockContent);
- // Si este segmento esta vacio del todo, usa la textura adecuada
- } else {
- tx = 0;
- }
- } else {
- tx = 0;
- }
- // Dibuja todos los elementos de la barra
- GUI.DrawTexture(new Rect(posX, 24, txAmmoBarLed[tx].width, txAmmoBarLed[tx].height), txAmmoBarLed[tx], ScaleMode.ScaleToFit, true, 0);
- posX += ammoBarSpace;
- }
- // Marcador digital de la municion
- GUI.Label(new Rect(340, 20, 100, 50), Player.ammo.ToString("000"), skAmmo);
- GUI.EndGroup();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement