Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class GuiMsgBox : MonoBehaviour {
- // Copia static de el mismo
- private static GuiMsgBox mySelf;
- // Variables para la caja de texto
- public struct msgBoxStruct {
- public string text; // Texto que contendra
- public int posX; // Posicion
- public int posY;
- public int width; // Tamaño
- public int height;
- public bool show; // Flag de visibilidad
- public bool autoDestroy; // Flag de autodestruccion del texto
- public float startTime; // Tiempo de vida
- public float endTime;
- public float liveTime;
- };
- public msgBoxStruct msgBox; // La variable "textBox" ahora es del tipo "textBoxStruct"
- public GUIStyle skMsgBox = null;
- // Variables de control
- public bool useFade; // Flag de uso del efecto fade en los cuadros de textos
- public float fadeLive; // Duracion el efecto fade (en segundos)
- private float fadeTime; // Control del tiempo de ejecucion del fade
- private float fadeElapsed; // Tiempo de fade transcurrido
- private float currentFade; // Nivel actual de fade
- private Color textColor; // Control del color de la caja de texto
- // Variables de la maquina es estados
- private const uint onCreate = 0;
- private const uint fadeIn = 1;
- private const uint fadeOut = 2;
- private const uint wait = 3;
- private const uint idle = 4;
- private const uint destroy = 5;
- private uint thread = 0, nextThread = 0;
- // Use this for initialization
- void Start () {
- // Autocopia de el mismo
- mySelf = this;
- // Inicia las variables importantes
- fadeElapsed = fadeLive;
- }
- // Main GUI
- void OnGUI() {
- // Genera una ventana de texto si es necesario
- if (msgBox.show) DrawGui();
- }
- // Dibuja una caja de texto
- void DrawGui() {
- // Variables privadas de la funcion
- float tempFadeLevel = 0f;
- // Maquina de estados del Msg Box
- switch (thread) {
- // Al crearse
- case onCreate:
- // Dependiendo de si esta activo el flag de efecto fade...
- if (useFade) {
- // Calcula el tiempo de FADE
- fadeTime = (Time.timeSinceLevelLoad + fadeElapsed);
- // Calcula el nivel de alpha segun el tiempo
- tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
- // Invierte el Fade para el efecto fade in
- currentFade = (1f - tempFadeLevel);
- // Ajusta el nivel de alpha inicial
- textColor = new Color(1f, 1f, 1f, 0f);
- // Siguiente thread
- nextThread = fadeIn;
- } else {
- // Si no usas fade, dependiendo de si esta el autodestroy activo...
- textColor = new Color(1f, 1f, 1f, 1f);
- if (msgBox.autoDestroy) {
- // Calcula el tiempo de vida
- msgBox.startTime = Time.timeSinceLevelLoad;
- msgBox.endTime = (msgBox.startTime + msgBox.liveTime);
- // Siguiente thread
- nextThread = wait;
- } else {
- // Siguiente thread
- nextThread = idle;
- }
- }
- break;
- // Efecto fade in
- case fadeIn:
- if (Time.timeSinceLevelLoad <= fadeTime) {
- // Calcula el nivel de alpha segun el tiempo
- tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
- // Invierte el Fade para el efecto fade in
- currentFade = (1f - tempFadeLevel);
- // Calcula el nivel de fade en relacion al ultimo frame para poder invertir el efecto
- fadeElapsed = (fadeLive - (fadeLive - (fadeTime - Time.timeSinceLevelLoad)));
- // Aplica el nivel de alpha
- textColor = new Color(1f, 1f, 1f, currentFade);
- } else {
- // Ajusta el alpha a opcaco
- textColor = new Color(1f, 1f, 1f, 1f);
- fadeElapsed = 0f;
- // Si no usas fade, dependiendo de si esta el autodestroy activo...
- if (msgBox.autoDestroy) {
- // Calcula el tiempo de vida
- msgBox.startTime = Time.timeSinceLevelLoad;
- msgBox.endTime = (msgBox.startTime + msgBox.liveTime);
- // Siguiente thread
- nextThread = wait;
- } else {
- // Siguiente thread
- nextThread = idle;
- }
- }
- break;
- // Efecto fade out
- case fadeOut:
- if (Time.timeSinceLevelLoad <= fadeTime) {
- // Calcula el nivel de alpha segun el tiempo
- tempFadeLevel = ((fadeTime - Time.timeSinceLevelLoad) / fadeLive);
- // Invierte el Fade para el efecto fade in
- currentFade = tempFadeLevel;
- // Calcula el nivel de fade en relacion al ultimo frame para poder invertir el efecto
- fadeElapsed = (fadeLive - (fadeTime - Time.timeSinceLevelLoad));
- // Aplica el nivel de alpha
- textColor = new Color(1f, 1f, 1f, currentFade);
- } else {
- // Ajusta el alpha
- textColor = new Color(1f, 1f, 1f, 0f);
- fadeElapsed = fadeLive;
- // Destruye el texto
- nextThread = destroy;
- }
- break;
- // Espera
- case wait:
- // Control del tiempo de espera
- if (Time.timeSinceLevelLoad >= mySelf.msgBox.endTime) {
- // Dependiendo de si se usa el fade o no...
- if (useFade) {
- // Calcula el tiempo de FADE
- fadeTime = ((Time.timeSinceLevelLoad + fadeLive) - fadeElapsed);
- // Siguiente thread
- nextThread = fadeOut;
- } else {
- // Siguiente thread
- nextThread = destroy;
- }
- }
- break;
- // Sin hacer nada
- case idle:
- break;
- // Destruye el texto
- case destroy:
- mySelf.msgBox.text = string.Empty;
- mySelf.msgBox.show = false;
- mySelf.msgBox.autoDestroy = false;
- break;
- // Defecto, nada, error trap
- default:
- break;
- }
- // Crea el GUI label de las medidas indicadas (Imperativo, comun)
- GUI.color = textColor;
- GUI.Label(new Rect(msgBox.posX, msgBox.posY, msgBox.width, msgBox.height), msgBox.text, skMsgBox);
- // Siguiente thread
- if (nextThread != thread) thread = nextThread;
- }
- // Funcion para dibujar una caja de texto en la GUI
- public static void DrawMsgBox(int posX, int posY, int width, int height, string text, int liveTime) {
- // Guarda los parametros
- mySelf.msgBox.text = text;
- mySelf.msgBox.posX = posX;
- mySelf.msgBox.posY = posY;
- mySelf.msgBox.width = width;
- mySelf.msgBox.height = height;
- mySelf.msgBox.show = true;
- mySelf.msgBox.liveTime = liveTime;
- // Calculo del tiempo de vida
- if (liveTime > 0) {
- mySelf.msgBox.autoDestroy = true;
- } else {
- // Si es 0, no se autodestruye
- mySelf.msgBox.autoDestroy = false;
- }
- // Mandalo al thread inicial
- mySelf.thread = onCreate;
- mySelf.nextThread = onCreate;
- }
- // Funcion para borrar la caja de texto
- public static void EraseMsgBox() {
- // Dependiendo de si se usa el fade o no...
- if (mySelf.useFade) {
- // Si ya no te estas destruyendo...
- if ((mySelf.thread != fadeOut) && (mySelf.thread != destroy)) {
- mySelf.fadeTime = ((Time.timeSinceLevelLoad + mySelf.fadeLive) - mySelf.fadeElapsed);
- mySelf.nextThread = fadeOut;
- }
- } else {
- mySelf.nextThread = destroy;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement