【转】MessageBox脚本-U吧

/* 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;
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值