【转】MessageBox脚本-U吧

来源:互联网 发布:mac合盖不休眠 编辑:程序博客网 时间:2024/05/01 21:19

/* MESSAGE BOX SCRIPT
 * 
 * -----THE FIELDS REQUIRED FOR THIS FUNCTION--------------

 
 * face is the graphical icon of the character who is talking (these usually show their face, hence 'face').

nameF and textF determine the colors of the name of the person talking and the words they say respectively. 
 * They are determined by an enum called TextColor, which has a set of colors I can choose from (Blue, Cyan, 
 * Dark Green, Dark Red, Golden, Gray, Green, Red, Teal, and Purple). These are the colors I plan on using 
 * in my game, they may differ from yours, but you can easily change or add more colors to the enum (I do 
 * recommend only using a finite number of colors in your dialogs - I'd hate to have the colors keep 
 * switching on me for every different person who talks! :P )

messagePos is a Nullable enum parameter which dictates where (vertically speaking) the message is displayed . 
 * If it is null, then the message is shown at the bottom of the screen.

mName is the name of the character speaking
 
boxShow decides whether the box is opaque or invisible.
 
_text is what the character actually says
 
buttons is in array of up to four strings. These hold the text for the user's available responses. The number 
 * of buttons shown will match the size of this array.
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class MessageBoxScript : MonoBehaviour
{
    //ENUMS
    public enum Position { Top, Middle, Bottom }
    public enum TextColor { Red, Blue, Green, Cyan, Purple,
        DarkGreen, Goldenrod, DarkRed, Teal, Black, Gray}

    //VALUES AND STRUCTS
    const int BOX_LENGTH = 600;
    const int BOX_HEIGHT = 100;
    const int BUTTON_WIDTH = 300;
    const int BUTTON_HEIGHT = 30;
    const int FACE_SIDE = 50;

    static GUISkin cyanGUI;
    static string msgName, buttonText1, buttonText2, buttonText3, buttonText4, wordLabel, message;
    static bool[] mGo;
    static bool showText, showBox, showButton;
    public static float letterPause = 0.01f;
    static GUIStyle nameFont, textFont;
    static Texture face;
    static float letterPause1;
    static float letterPause2 = 0.25f;
    static int messageBox_xPos, messageBox_yPos, numButtons;
    static string[] continues;

    void Awake()
    {
        continues = new string[4];
        mGo = new bool[4];
        nameFont = new GUIStyle();
        textFont = new GUIStyle();
    }

    /// <summary>
    /// Will display a message box.
    /// </summary>
    /// <param name="face_">the graphical icon of the character who is talking</param>
    /// <param name="nameF">determine the color of the name of the person Talking</param>
    /// <param name="mName">The name of the person talking </param>
    /// <param name="textF">Whether to make the box transparent or opaque</param>
    /// <param name="messagePos">Where the message is displayed on the screen</param>
    /// <param name="boxShow">Show the message box or make it transparent?</param>
    /// <param name="_text">The string to display in the message</param>
    /// <param name="buttons">The text displayed on the buttons</param>
    public static void MessageBox(Texture face_, TextColor nameF, 
        TextColor textF, Position? messagePos, string mName, bool boxShow, string _text, 
        string[] buttons)
    {
        if (buttons.Length > 4)
        {
            Debug.LogError("MessageBox Cannot display more than 4 buttons!");
            return;
        }
        wordLabel = "";
        showText = true;
        showButton = false;
        nameFont.normal.textColor = ConfigureColor(nameF);
        textFont.normal.textColor = ConfigureColor(textF);
        textFont.wordWrap = true;
        textFont.fixedWidth = 540;
        face = face_;
        showBox = boxShow; //Display the window?
        ConfigurePosition(messagePos);
        msgName = mName; //get the Name
        message = _text; //get the message text
        continues = buttons; //How many responses?
    }

    public static Color ConfigureColor(TextColor color)
    {
        switch (color)
        {
            case TextColor.Blue:
                return Color.blue;
            case TextColor.Cyan:
                return Color.cyan;
            case TextColor.DarkGreen:
                return new Color(.2f, .5f, 0);
            case TextColor.DarkRed:
                return new Color(.5f, 0, 0);
            case TextColor.Goldenrod:
                return new Color(.855f, .647f, .126f);
            case TextColor.Gray:
                return Color.gray;
            case TextColor.Green:
                return Color.green;
            case TextColor.Red:
                return Color.red;
            case TextColor.Teal:
                return new Color(0, .5f, .5f);
            case TextColor.Purple:
                return new Color(.6f, 0, 1);
            default:
                return Color.black;
        }
    }

    public static void ConfigurePosition(Position? pos)
    {
        messageBox_xPos = (Screen.width / 2) - (BOX_LENGTH / 2); //Make the window in the center of the screen
        if (pos == Position.Middle) //Message at the middle of the screen
        {
            messageBox_yPos = (Screen.height / 2) - (BOX_HEIGHT / 2);
        }
        else if (pos == Position.Top) //Message at the top
        {
            messageBox_yPos = 30;
        }
        else  //Message at the bottom (default)
        {
            messageBox_yPos = Screen.height - BOX_HEIGHT - (2 * BUTTON_HEIGHT);
        }
    }

    /// <summary>
    /// Will display the characters in a typewriter effect
    /// </summary>
    /// <param name="m">The string to parse and display</param>
    IEnumerator TypeText(string m)
    {
        for (int index = 0; index < m.Length; index++)
        {
            letterPause1 = letterPause; //Make the pause the default time
            if (m[index] == '$')
            {
                letterPause1 = letterPause2; //Make the pause longer
            }
            else if (m[index] == '~')
            {
                wordLabel += '\n';
            }
            else
            {
                wordLabel += m[index]; //Add the current letter to the sentence
            }
            yield return new WaitForSeconds(letterPause1);
        }
        yield return StartCoroutine(ContinueButton()); //Call the ContinueButton method
        showButton = true; //Show the GUI button
    }

    //Wait to show the Continue button
    IEnumerator ContinueButton()
    {
        yield return new WaitForSeconds(0.1f); //Wait for 0.1 seconds
    }

    // Update is called once per frame
    void OnGUI()
    {
        if (showBox)
        { //If the Show Box flag is selected. 
            GUI.Box(new Rect(messageBox_xPos, messageBox_yPos, BOX_LENGTH, BOX_HEIGHT), ""); //The message box
        }
        //Below is where the name goes
        GUI.Label(new Rect((messageBox_xPos + FACE_SIDE + 10), (messageBox_yPos + 3), (BOX_LENGTH - 30), (BOX_HEIGHT - 10)), msgName, nameFont);
        //Below is where the text goes
        GUI.Label(new Rect((messageBox_xPos + FACE_SIDE + 10), (messageBox_yPos + 28), (BOX_LENGTH - 30), (BOX_HEIGHT - 10)), wordLabel, textFont);
        //Below is there the face graphic goes
        GUI.DrawTexture(new Rect(messageBox_xPos + 10, messageBox_yPos + 3, FACE_SIDE, FACE_SIDE), face, ScaleMode.StretchToFill, true, 0);
        DisplayInputButtons(continues);
    }

    void Update()
    {
        if (showText)
        {
            StartCoroutine(TypeText(message));
            showText = false;
        }
    }

    //Return the integer mapping to the response
    public static int getCont()
    {
        for (int i = 0; i < mGo.Length; i++)
        {
            if (mGo[i] == true)
            {
                return i;
            }
        }
        return -1;
    }

    public static void ResetButton()
    {
        for (int i = 0; i < mGo.Length; i++)
        {
            if (mGo[i] == true) mGo[i] = false;
        }
    }

    /// <summary>
    /// Will display the response options
    /// </summary>
    /// <param name="buttons">The array of strings for the buttons</param>
    void DisplayInputButtons(string[] buttons)
    {
        Rect[] butPos = new Rect[buttons.Length];
        int length = 0;
        //Determine which display to show based on which button strings are null.
        for (int i = 0; i < buttons.Length; i++)
        {
            if (buttons[i] != null) length = i + 1;
        }
        switch(length)
        {
            case 1:
                butPos[0] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - (BUTTON_WIDTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                break;

            case 2:
                butPos[0] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[1] = new Rect(messageBox_xPos + (BOX_LENGTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                break;

            case 3:
                butPos[0] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[1] = new Rect(messageBox_xPos + (BOX_LENGTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[2] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - (BUTTON_WIDTH / 2), messageBox_yPos + BOX_HEIGHT + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                break;

            case 4:
                butPos[0] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[1] = new Rect(messageBox_xPos + (BOX_LENGTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[2] = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos[3] = new Rect(messageBox_xPos + (BOX_LENGTH / 2), messageBox_yPos + BOX_HEIGHT + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                break;
        }
        if (showButton)
        {
            for (int i = 0; i < buttons.Length; i++)
            {
                mGo[i] = GUI.Button(butPos[i], buttons[i]);
                if (mGo[i]) cyanGUI = null;
            }
        }
    }
}
 

原创粉丝点击