monery8 发表于 2012-11-26 11:40

Unity3D简单的对话框项目包

本帖最后由 monery8 于 2012-11-26 11:42 编辑



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;
      mGo = new bool;
      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 == '$')
            {
                letterPause1 = letterPause2; //Make the pause longer
            }
            else if (m == '~')
            {
                wordLabel += '\n';
            }
            else
            {
                wordLabel += m; //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 == true)
            {
                return i;
            }
      }
      return -1;
    }

    public static void ResetButton()
    {
      for (int i = 0; i < mGo.Length; i++)
      {
            if (mGo == true) mGo = 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;
      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 != null) length = i + 1;
      }
      switch(length)
      {
            case 1:
                butPos = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - (BUTTON_WIDTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                break;

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

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

            case 4:
                butPos = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos = new Rect(messageBox_xPos + (BOX_LENGTH / 2), messageBox_yPos + BOX_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos = new Rect(messageBox_xPos + (BOX_LENGTH / 2) - BUTTON_WIDTH, messageBox_yPos + BOX_HEIGHT + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
                butPos = 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 = GUI.Button(butPos, buttons);
                if (mGo) cyanGUI = null;
            }
      }
    }
}


using UnityEngine;
using System.Collections;

public class Program : MonoBehaviour
{
    public GUISkin cyanSkin;
    public Texture face;
    bool talk;
    string[] responses = { "Hello", "Bye" };
    string[] responses2 = { "Bye" };

    IEnumerator Start()
    {
      MessageBoxScript.MessageBox(face, MessageBoxScript.TextColor.Blue,
      MessageBoxScript.TextColor.Cyan, MessageBoxScript.Position.Bottom, "Foo-Man", true, "Hello World!",
      responses);
      talk = true;

      while (talk)
      {
            if (MessageBoxScript.getCont() == 0)
            {
                talk = false;
                MessageBoxScript.MessageBox(face, MessageBoxScript.TextColor.Blue,
                MessageBoxScript.TextColor.Cyan, MessageBoxScript.Position.Bottom, "Foo-Man", true, "Yay!$$ Blah blah blahbitty-blah blah blah!",
                responses2);
            }
            yield return null;
      }
    }
}

胡椒孙 发表于 2012-11-27 16:03

好长啊,拿回去慢慢看

utk 发表于 2013-2-4 13:32

thanks buddy

utk 发表于 2013-2-4 13:34

thanks buuddy

username 发表于 2013-7-29 08:42

学习学习,多谢分享!

yeahwin 发表于 2014-1-25 11:01

呃,就这样么?不过,还是谢谢楼主分享~

June 发表于 2017-4-15 21:08

很不错

roadProgram 发表于 2017-4-15 21:19

楼主是超人

karl 发表于 2017-4-15 21:12

顶顶多好

vv3918 发表于 2017-4-15 20:45

说的非常好
页: [1] 2
查看完整版本: Unity3D简单的对话框项目包