Advertisement
NightFox

Unity3d GUI - Textmsg Box

May 11th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.89 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GuiMsgBox : MonoBehaviour {
  5.    
  6.     // Copia static de el mismo
  7.     private static GuiMsgBox mySelf;
  8.    
  9.     // Variables para la caja de texto
  10.     public struct msgBoxStruct {
  11.         public string text;     // Texto que contendra
  12.         public int posX;        // Posicion
  13.         public int posY;
  14.         public int width;       // Tamaño
  15.         public int height;
  16.         public bool show;           // Flag de visibilidad
  17.         public bool autoDestroy;    // Flag de autodestruccion del texto
  18.         public float startTime;     // Tiempo de vida
  19.         public float endTime;
  20.         public float liveTime;
  21.     };
  22.     public msgBoxStruct msgBox;     // La variable "textBox" ahora es del tipo "textBoxStruct"
  23.     public GUIStyle skMsgBox = null;
  24.    
  25.     // Variables de control
  26.     public bool useFade;        // Flag de uso del efecto fade en los cuadros de textos
  27.     public float fadeLive;      // Duracion el efecto fade (en segundos)
  28.     private float fadeTime;     // Control del tiempo de ejecucion del fade
  29.     private float fadeElapsed;  // Tiempo de fade transcurrido
  30.     private float currentFade;  // Nivel actual de fade
  31.     private Color textColor;    // Control del color de la caja de texto
  32.    
  33.     // Variables de la maquina es estados
  34.     private const uint onCreate = 0;
  35.     private const uint fadeIn = 1;
  36.     private const uint fadeOut = 2;
  37.     private const uint wait = 3;
  38.     private const uint idle = 4;
  39.     private const uint destroy = 5;
  40.     private uint thread = 0, nextThread = 0;
  41.    
  42.  
  43.    
  44.  
  45.     // Use this for initialization
  46.     void Start () {
  47.        
  48.         // Autocopia de el mismo
  49.         mySelf = this;
  50.        
  51.         // Inicia las variables importantes
  52.         fadeElapsed = fadeLive;
  53.    
  54.     }
  55.    
  56.    
  57.    
  58.     // Main GUI
  59.     void OnGUI() {
  60.        
  61.         // Genera una ventana de texto si es necesario
  62.         if (msgBox.show) DrawGui();
  63.        
  64.     }
  65.    
  66.    
  67.    
  68.     // Dibuja una caja de texto
  69.     void DrawGui() {
  70.        
  71.         // Variables privadas de la funcion
  72.         float tempFadeLevel = 0f;
  73.                
  74.         // Maquina de estados del Msg Box
  75.         switch (thread) {
  76.            
  77.             // Al crearse
  78.             case onCreate:
  79.                 // Dependiendo de si esta activo el flag de efecto fade...
  80.                 if (useFade) {
  81.                     // Calcula el tiempo de FADE
  82.                     fadeTime = (Time.timeSinceLevelLoad + fadeElapsed);
  83.                     // Calcula el nivel de alpha segun el tiempo
  84.                     tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
  85.                     // Invierte el Fade para el efecto fade in
  86.                     currentFade = (1f - tempFadeLevel);
  87.                     // Ajusta el nivel de alpha inicial
  88.                     textColor = new Color(1f, 1f, 1f, 0f);
  89.                     // Siguiente thread
  90.                     nextThread = fadeIn;
  91.                 } else {
  92.                     // Si no usas fade, dependiendo de si esta el autodestroy activo...
  93.                     textColor = new Color(1f, 1f, 1f, 1f);
  94.                     if (msgBox.autoDestroy) {
  95.                         // Calcula el tiempo de vida
  96.                         msgBox.startTime = Time.timeSinceLevelLoad;
  97.                         msgBox.endTime = (msgBox.startTime + msgBox.liveTime);
  98.                         // Siguiente thread
  99.                         nextThread = wait;
  100.                     } else {
  101.                         // Siguiente thread
  102.                         nextThread = idle;
  103.                     }
  104.                 }
  105.                 break;
  106.            
  107.             // Efecto fade in
  108.             case fadeIn:
  109.                 if (Time.timeSinceLevelLoad <= fadeTime) {
  110.                     // Calcula el nivel de alpha segun el tiempo
  111.                     tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
  112.                     // Invierte el Fade para el efecto fade in
  113.                     currentFade = (1f - tempFadeLevel);
  114.                     // Calcula el nivel de fade en relacion al ultimo frame para poder invertir el efecto
  115.                     fadeElapsed = (fadeLive - (fadeLive - (fadeTime - Time.timeSinceLevelLoad)));
  116.                     // Aplica el nivel de alpha
  117.                     textColor = new Color(1f, 1f, 1f, currentFade);
  118.                 } else {
  119.                     // Ajusta el alpha a opcaco
  120.                     textColor = new Color(1f, 1f, 1f, 1f);
  121.                     fadeElapsed = 0f;
  122.                     // Si no usas fade, dependiendo de si esta el autodestroy activo...
  123.                     if (msgBox.autoDestroy) {
  124.                         // Calcula el tiempo de vida
  125.                         msgBox.startTime = Time.timeSinceLevelLoad;
  126.                         msgBox.endTime = (msgBox.startTime + msgBox.liveTime);
  127.                         // Siguiente thread
  128.                         nextThread = wait;
  129.                     } else {
  130.                         // Siguiente thread
  131.                         nextThread = idle;
  132.                     }
  133.                 }
  134.                 break;
  135.                
  136.             // Efecto fade out
  137.             case fadeOut:
  138.                 if (Time.timeSinceLevelLoad <= fadeTime) {
  139.                     // Calcula el nivel de alpha segun el tiempo
  140.                     tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
  141.                     // Invierte el Fade para el efecto fade in
  142.                     currentFade = tempFadeLevel;
  143.                     // Calcula el nivel de fade en relacion al ultimo frame para poder invertir el efecto
  144.                     fadeElapsed = (fadeLive - (fadeTime - Time.timeSinceLevelLoad));
  145.                     // Aplica el nivel de alpha
  146.                     textColor = new Color(1f, 1f, 1f, currentFade);
  147.                 } else {
  148.                     // Ajusta el alpha
  149.                     textColor = new Color(1f, 1f, 1f, 0f);
  150.                     fadeElapsed = fadeLive;
  151.                     // Destruye el texto
  152.                     nextThread = destroy;
  153.                 }
  154.                 break;
  155.                
  156.             // Espera
  157.             case wait:
  158.                 // Control del tiempo de espera
  159.                 if (Time.timeSinceLevelLoad >= mySelf.msgBox.endTime) {
  160.                     // Dependiendo de si se usa el fade o no...
  161.                     if (useFade) {
  162.                         // Calcula el tiempo de FADE
  163.                         fadeTime = ((Time.timeSinceLevelLoad + fadeLive) - fadeElapsed);
  164.                         // Siguiente thread
  165.                         nextThread = fadeOut;
  166.                     } else {
  167.                         // Siguiente thread
  168.                         nextThread = destroy;
  169.                     }
  170.                 }
  171.                 break;
  172.                
  173.             // Sin hacer nada
  174.             case idle:
  175.                 break;
  176.                
  177.             // Destruye el texto
  178.             case destroy:
  179.                 mySelf.msgBox.text = string.Empty;
  180.                 mySelf.msgBox.show = false;
  181.                 mySelf.msgBox.autoDestroy = false;
  182.                 break;
  183.                
  184.             // Defecto, nada, error trap
  185.             default:
  186.                 break;
  187.            
  188.         }
  189.        
  190.        
  191.         // Crea el GUI label de las medidas indicadas (Imperativo, comun)
  192.         GUI.color = textColor;
  193.         GUI.Label(new Rect(msgBox.posX, msgBox.posY, msgBox.width, msgBox.height), msgBox.text, skMsgBox);
  194.  
  195.         // Siguiente thread
  196.         if (nextThread != thread) thread = nextThread;
  197.  
  198.     }
  199.    
  200.    
  201.    
  202.     // Funcion para dibujar una caja de texto en la GUI
  203.     public static void DrawMsgBox(int posX, int posY, int width, int height, string text, int liveTime) {
  204.        
  205.         // Guarda los parametros
  206.         mySelf.msgBox.text = text;
  207.         mySelf.msgBox.posX = posX;
  208.         mySelf.msgBox.posY = posY;
  209.         mySelf.msgBox.width = width;
  210.         mySelf.msgBox.height = height;
  211.         mySelf.msgBox.show = true;
  212.         mySelf.msgBox.liveTime = liveTime;
  213.        
  214.         // Calculo del tiempo de vida
  215.         if (liveTime > 0) {
  216.             mySelf.msgBox.autoDestroy = true;
  217.         } else {
  218.             // Si es 0, no se autodestruye
  219.             mySelf.msgBox.autoDestroy = false;
  220.         }
  221.        
  222.         // Mandalo al thread inicial
  223.         mySelf.thread = onCreate;
  224.         mySelf.nextThread = onCreate;
  225.        
  226.     }
  227.    
  228.    
  229.    
  230.     // Funcion para borrar la caja de texto
  231.     public static void EraseMsgBox() {
  232.         // Dependiendo de si se usa el fade o no...
  233.         if (mySelf.useFade) {
  234.             // Si ya no te estas destruyendo...
  235.             if ((mySelf.thread != fadeOut) && (mySelf.thread != destroy)) {
  236.                 mySelf.fadeTime = ((Time.timeSinceLevelLoad + mySelf.fadeLive) - mySelf.fadeElapsed);
  237.                 mySelf.nextThread = fadeOut;
  238.             }
  239.         } else {
  240.             mySelf.nextThread = destroy;
  241.         }
  242.     }
  243.    
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement