|
资源信息 Asset Information
资源名称: | Unity3D简单的对话框项目包 (发帖教程) |
版本: | 无 (版本) |
资源等级: | 1 |
资源格式: | .rar (链接失效请点击帖子右下方举报通知管理员) |
资源大小: | 2MB (默认:MB) |
下载地址: | 站内下载 (购买积分) |
本帖最后由 monery8 于 2012-11-26 11:42 编辑
[code=csharp]
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 == 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[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 != 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 = GUI.Button(butPos, buttons);
if (mGo) cyanGUI = null;
}
}
}
}
[/code]
[code=csharp]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;
}
}
}
[/code]
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|