经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
unity 之 自定义弹出框
来源:cnblogs  作者:无名之士  时间:2019/11/4 8:29:43  对本文有异议
一、弹出框的搭建:

布局如图:Message为整个父物体,并且添加UiMessage代码。panel为遮罩。

MessageBox为整个提示框,Panel为标题,ok为确定按钮,cancel为取消按钮,retry为重试按钮,Text为提示框的文字。

注意大小写,后面代码会根据名称进行获取对应组建。

 

 效果如下:

 

 

 

二、MessageBox代码:

 要说明的都在代码中注释了。仿照Windows的提示框功能,如果功能不足可自行添加。例如关闭按钮、显示图标等。

  1. using System;
  2. public enum DialogResult
  3. {
  4. Ok,
  5. OKCancel,
  6. RetryCancel,
  7. YesNo,
  8. YesNoCancel
  9. }
  10. public static class MessageBox
  11. {
  12. /// <summary>
  13. /// true表示模态框
  14. /// </summary>
  15. public static bool type;
  16. //三个委托,分别为三个按钮的点击运行事件
  17. public static Action clickOk;
  18. public static Action clickRetry;
  19. public static Action clickCancel;
  20. public static DialogResult dialogResult;
  21. //标题
  22. public static string headText;
  23. //文本
  24. public static string text;
  25. //状态。用于显示或隐藏弹出框
  26. public static bool state;
  27. /// <summary>
  28. ///重试按钮点击事件
  29. /// </summary>
  30. public static void onClickRetry()
  31. {
  32. state = false;
  33. clickRetry?.Invoke();
  34. clickRetry = null;
  35. }
  36. /// <summary>
  37. /// 取消按钮点击事件
  38. /// </summary>
  39. public static void onClickCancel()
  40. {
  41. state = false;
  42. clickCancel?.Invoke();
  43. clickCancel = null;
  44. }
  45. /// <summary>
  46. /// 确定按钮点击事件
  47. /// </summary>
  48. public static void onClickOk()
  49. {
  50. state = false;
  51. clickOk?.Invoke();
  52. clickOk = null;
  53. }
  54. /// <summary>
  55. /// 显示
  56. /// </summary>
  57. /// <param name="_text">内容</param>
  58. /// <param name="_head">标题</param>
  59. /// <param name="dialog">样式</param>
  60. /// <param name="type">模式</param>
  61. public static void Show(string _text,string _head,DialogResult _dialog, bool _type = true)
  62. {
  63. text = _text;
  64. headText = _head;
  65. dialogResult = _dialog;
  66. type = _type;
  67. state = true;
  68. }
  69. public static void Show(string _text,string _head,bool _type = true)
  70. {
  71. text = _text;
  72. headText = _head;
  73. dialogResult = DialogResult.Ok;
  74. type = _type;
  75. state = true;
  76. }
  77. public static void Show(string _text, bool _type = true)
  78. {
  79. text = _text;
  80. headText = "信息";
  81. dialogResult = DialogResult.Ok;
  82. type = _type;
  83. state = true;
  84. }
  85. }

 

三、UiMessage代码:

 添加到Message物体上。用于控制弹出框的显示等功能。

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class UiMessage : MonoBehaviour
  4. {
  5. public Button ok;
  6. public Button cancel;
  7. public Button retry;
  8. /// <summary>
  9. /// 遮罩
  10. /// </summary>
  11. public GameObject panel;
  12. public Text headText;
  13. public Text text;
  14. /// <summary>
  15. /// 弹出框
  16. /// </summary>
  17. private GameObject messageBox;
  18. private void Awake()
  19. {
  20. messageBox = gameObject.transform.GetChild(1).gameObject;
  21. ok = messageBox.transform.Find("ok").GetComponent<Button>();
  22. cancel = messageBox.transform.Find("cancel").GetComponent<Button>();
  23. retry = messageBox.transform.Find("retry").GetComponent<Button>();
  24. panel = gameObject.transform.Find("panel").gameObject;
  25. text = messageBox.transform.Find("Text").GetComponent<Text>();
  26. headText = messageBox.transform.GetChild(0).Find("head").GetComponent<Text>();
  27. //将提示框居中显示
  28. messageBox.transform.position = new Vector3(Screen.width / 2 - messageBox.GetComponent<RectTransform>().rect.width / 2,
  29. Screen.height / 2 + messageBox.GetComponent<RectTransform>().rect.height / 2, 0);
  30. init();
  31. }
  32. private void OnEnable()
  33. {
  34. init();
  35. }
  36. private void init()
  37. {
  38. ok.onClick.AddListener(MessageBox.onClickOk);
  39. cancel.onClick.AddListener(MessageBox.onClickCancel);
  40. retry.onClick.AddListener(MessageBox.onClickRetry);
  41. text.text = MessageBox.text;
  42. headText.text = MessageBox.headText;
  43. //根据传递的参数,进行样式的显示
  44. switch (MessageBox.dialogResult)
  45. {
  46. case DialogResult.Ok:
  47. ok.gameObject.SetActive(true);
  48. cancel.gameObject.SetActive(false);
  49. retry.gameObject.SetActive(false);
  50. break;
  51. case DialogResult.OKCancel:
  52. ok.gameObject.SetActive(true);
  53. cancel.gameObject.SetActive(true);
  54. retry.gameObject.SetActive(false);
  55. break;
  56. case DialogResult.RetryCancel:
  57. ok.gameObject.SetActive(true);
  58. cancel.gameObject.SetActive(true);
  59. retry.gameObject.SetActive(true);
  60. break;
  61. case DialogResult.YesNo:
  62. ok.transform.GetChild(0).GetComponent<Text>().text = "";
  63. cancel.transform.GetChild(0).GetComponent<Text>().text = "";
  64. ok.gameObject.SetActive(true);
  65. cancel.gameObject.SetActive(true);
  66. retry.gameObject.SetActive(false);
  67. break;
  68. case DialogResult.YesNoCancel:
  69. ok.transform.GetChild(0).GetComponent<Text>().text = "";
  70. cancel.transform.GetChild(0).GetComponent<Text>().text = "";
  71. ok.gameObject.SetActive(true);
  72. cancel.gameObject.SetActive(true);
  73. retry.gameObject.SetActive(true);
  74. break;
  75. }
  76. }
  77. private void Update()
  78. {
  79. panel.SetActive(MessageBox.type);
  80. gameObject.SetActive(MessageBox.state);
  81. }
  82. }

 

三、显示框的调用

此处调用可以自行设置一个按钮,在其点击事件中注册调用即可。

笔者使用项目中的方式进行演示。具体不做说明。调用方式已给出。

特别注意:由于UiMessage调用了MessageBox的方法,所以必须先初始化MessageBox的数据。使用什么就初始化什么。笔者使用了ok、cancel按钮(默认不初始化模式,即为模态框,不初始化DialogResult即为只显示ok按钮),所以注册了相应的点击事件(委托)。最后显示弹出框(整个包含遮罩和弹出框)。

 

 

 

三、运行结果

 

 

 

三、弹出框可拖拽移动

将DragManage添加到MessageBox物体上面。(如果你想让ui物体可拖拽,对其添加DragManage即可实现)

笔者就不做演示了

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. /// <summary>
  4. /// 只是用来处理拖拽
  5. /// </summary>
  6. public class DragManage : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
  7. {
  8. private Vector3 offect;
  9. public void OnBeginDrag(PointerEventData eventData)
  10. {
  11. offect = Input.mousePosition - transform.position;
  12. }
  13. public void OnDrag(PointerEventData eventData)
  14. {
  15. transform.position = Input.mousePosition - offect;
  16. }
  17. public void OnEndDrag(PointerEventData eventData)
  18. {
  19. transform.position = Input.mousePosition - offect;
  20. }
  21. }

 

 

原文链接:http://www.cnblogs.com/unknown6248/p/11784813.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号