关于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 }
代码是很清晰了,至于不懂得,欢迎交流!
广告之后再回来!
相关文章推荐
- hdu 2108
- 声场模型如何区分近场和远场
- Linux操作系统中JDK的安装
- HDU 3368 Reversi (暴力,DFS)
- Android Glide图片加载库的使用
- ServletInputStream的重复读取(多次读取)(转)
- BaseAdapter的使用及优化
- python获取系统硬件信息
- java编程---3.1 (关于String的练习题) 将字符串中的字符按需求等分,例如:把aqzxswerdcvfr变成AQZX-SWER-DCVF-R
- 对区域的操作
- 进程间通信(IPC)(2)—— 消息队列
- php基础部分总结篇-魔术方法
- 一些 Windows 系统不常见的 鼠标光标常数
- Python BeautifulSoup中文乱码问题的2种解决方法
- link、unlink、remove和rename
- Java源码-N个数字中第k个最大值的求解方法
- grails中Template
- 深入理解JVM 第一章之1
- struts2+hibernate环境搭建
- smarty变量