您的位置:首页 > 其它

关于MessageBox消息弹出框的制作

2016-07-26 23:24 363 查看
广告结束回来,精彩继续!

我们游戏中存在好多种消息机制即二次确认框,这也是游戏中非常重要的一部分,这可以给玩家带来更好的体验,而我们做游戏的目标不就是给玩家创造美好的体验么?

一下是我在开发项目中开发的一个提示框组件即二次确认框:

Enum MessageBoxButtons
{
OK,
Cancel,
Sure
}


using UnityEngine;
using System;
using System.Collections;

// 对话提示框显示组件
public class MessageBox : MonoBehaviour
{
public static MessageBox Instance { get { return instance; } }

private static MessageBox instance = null;

// 是否点击任意处关闭对话提示框
private bool isAnywhereQuit;

private GameObject goOK;
private GameObject goCancel;
private GameObject goSure;

private UILabel labelMessage;
private UILabel labelOK;
private UILabel labelCancel;
private UILabel labelSure;

<span style="white-space:pre">	</span>// 都是点击确认,取消还有空白处的回调函数
private Action firstCallback;
private Action secondCallback;
private Action thirdCallback;
private Action callback;
private Action anyWhereCallback;

void Awake()
{
instance = this;

var refs                = GetComponent<GameObjectRef>();

goOK                    = refs["OK"];
goCancel                = refs["Cancel"];
goSure                  = refs["Sure"];

goOK.SetActive(false);
goCancel.SetActive(false);
goSure.SetActive(false);

labelMessage            = refs["Label_Message"].GetComponent<UILabel>();
labelOK                 = refs["Label_OK"].GetComponent<UILabel>();
labelCancel             = refs["Label_Cancel"].GetComponent<UILabel>();
labelSure               = refs["Label_Sure"].GetComponent<UILabel>();
}

void OnDisable()
{
isAnywhereQuit = false;
}

public void Show(string message, MessageBoxButtons firstButton, Action firstCallback,
MessageBoxButtons secondButton, Action secondCallback, Action callback, bool isAnywhereQuit = false)
{
Show(message, firstButton, firstCallback, secondButton, secondCallback, isAnywhereQuit);
this.callback = callback;
}

// 显示三个按钮
public void Show(string message, string firstButtonText, Action firstCallback,
string secondButtonText, Action secondCallback,
string thirdButtonText, Action thirdCallback,
bool isAnywhereQuit = false, Action anyWhereCallback = null)
{
showMessageBox(true);

labelMessage.text = message;
labelOK.text = firstButtonText;
labelCancel.text = secondButtonText;
labelSure.text = thirdButtonText;

this.firstCallback = firstCallback;
this.secondCallback = secondCallback;
this.thirdCallback = thirdCallback;
this.isAnywhereQuit = isAnywhereQuit;
this.anyWhereCallback = anyWhereCallback;

goOK.SetActive(true);
goCancel.SetActive(true);
goSure.SetActive(true);
}

// 显示两个按钮
public void Show(string message, MessageBoxButtons firstButton, Action firstCallback,
MessageBoxButtons secondButton, Action secondCallback, bool isAnywhereQuit = false, Action anyWhereCallback = null)
{
showMessageBox(true);

labelMessage.text = message;
showButtonLabelText(labelOK, firstButton);
showButtonLabelText(labelCancel, secondButton);

this.firstCallback = firstCallback;
this.secondCallback = secondCallback;
this.isAnywhereQuit = isAnywhereQuit;
this.anyWhereCallback = anyWhereCallback;

goOK.SetActive(true);
goCancel.SetActive(true);
goSure.SetActive(false);
}

// 只显示一个按钮
public void Show(string message, MessageBoxButtons button, Action callback, bool isAnywhereQuit = false)
{
showMessageBox(true);

labelMessage.text = message;
showButtonLabelText(labelSure, button);

this.thirdCallback = callback;
this.isAnywhereQuit = isAnywhereQuit;

goOK.SetActive(false);
goCancel.SetActive(false);
goSure.SetActive(true);
}

public void ShowMessageBox(bool isActive)
{
showMessageBox(isActive);
}

public GameObject GetOKButton()
{
return goOK;
}

private void showMessageBox(bool isActive)
{
this.gameObject.SetActive(isActive);
GUIResource.ToggleTooltipUICamera(isActive);
}

// 根据按钮类型,显示按钮label上的文字
private void showButtonLabelText(UILabel label, MessageBoxButtons button)
{
switch(button)
{
case MessageBoxButtons.OK:
label.text = StringTable.ST_OK;
break;

case MessageBoxButtons.Cancel:
label.text = StringTable.ST_CANCEL;
break;

case MessageBoxButtons.Yes:
label.text = StringTable.ST_YES;
break;

case MessageBoxButtons.No:
label.text = StringTable.ST_NO;
break;

default:
break;
}
}

private void onClickOKButton()
{
showMessageBox(false);

if (firstCallback != null)
{
firstCallback();
}

if (callback != null)
{
callback();
callback = null;
}
}

private void onClickCancelButton()
{
showMessageBox(false);

if (secondCallback != null)
{
secondCallback();
}

if (callback != null)
{
callback();
callback = null;
}
}

private void onClickSureButton()
{
showMessageBox(false);

if (thirdCallback != null)
{
thirdCallback();
thirdCallback = null;
}

if (callback != null)
{
callback();
callback = null;
}
}

private void onClickMessageBox()
{
if (isAnywhereQuit)
{
showMessageBox(false);

if (anyWhereCallback != null)
{
anyWhereCallback();
anyWhereCallback = null;
}
}
}
// end of class
}


代码是很清晰了,至于不懂得,欢迎交流!

广告之后再回来!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: