-
C#教程之unity自定义弹出框功能
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
本文实例为大家分享了unity自定义弹出框的具体方法,供大家参考,具体内容如下
一、弹出框的搭建
布局如图:Message为整个父物体,并且添加UiMessage代码。panel为遮罩。
MessageBox为整个提示框,Panel为标题,ok为确定按钮,cancel为取消按钮,retry为重试按钮,Text为提示框的文字。
注意大小写,后面代码会根据名称进行获取对应组建。
效果如下:
二、MessageBox代码
要说明的都在代码中注释了。仿照Windows的提示框功能,如果功能不足可自行添加。例如关闭按钮、显示图标等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
using System; public enum DialogResult { Ok, OKCancel, RetryCancel, YesNo, YesNoCancel } public static class MessageBox { /// <summary> /// true表示模态框 /// </summary> public static bool type; //三个委托,分别为三个按钮的点击运行事件 public static Action clickOk; public static Action clickRetry; public static Action clickCancel; public static DialogResult dialogResult; //标题 public static string headText; //文本 public static string text; //状态。用于显示或隐藏弹出框 public static bool state; /// <summary> ///重试按钮点击事件 /// </summary> public static void onClickRetry() { state = false ; clickRetry?.Invoke(); clickRetry = null ; } /// <summary> /// 取消按钮点击事件 /// </summary> public static void onClickCancel() { state = false ; clickCancel?.Invoke(); clickCancel = null ; } /// <summary> /// 确定按钮点击事件 /// </summary> public static void onClickOk() { state = false ; clickOk?.Invoke(); clickOk = null ; } /// <summary> /// 显示 /// </summary> /// <param name="_text">内容</param> /// <param name="_head">标题</param> /// <param name="dialog">样式</param> /// <param name="type">模式</param> public static void Show( string _text, string _head,DialogResult _dialog, bool _type = true ) { text = _text; headText = _head; dialogResult = _dialog; type = _type; state = true ; } public static void Show( string _text, string _head, bool _type = true ) { text = _text; headText = _head; dialogResult = DialogResult.Ok; type = _type; state = true ; } public static void Show( string _text, bool _type = true ) { text = _text; headText = "信息" ; dialogResult = DialogResult.Ok; type = _type; state = true ; } } |
三、UiMessage代码
添加到Message物体上。用于控制弹出框的显示等功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
using UnityEngine; using UnityEngine.UI; public class UiMessage : MonoBehaviour { public Button ok; public Button cancel; public Button retry; /// <summary> /// 遮罩 /// </summary> public GameObject panel; public Text headText; public Text text; /// <summary> /// 弹出框 /// </summary> private GameObject messageBox; private void Awake() { messageBox = gameObject.transform.GetChild(1).gameObject; ok = messageBox.transform.Find( "ok" ).GetComponent<Button>(); cancel = messageBox.transform.Find( "cancel" ).GetComponent<Button>(); retry = messageBox.transform.Find( "retry" ).GetComponent<Button>(); panel = gameObject.transform.Find( "panel" ).gameObject; text = messageBox.transform.Find( "Text" ).GetComponent<Text>(); headText = messageBox.transform.GetChild(0).Find( "head" ).GetComponent<Text>(); //将提示框居中显示 messageBox.transform.position = new Vector3(Screen.width / 2 - messageBox.GetComponent<RectTransform>().rect.width / 2, Screen.height / 2 + messageBox.GetComponent<RectTransform>().rect.height / 2, 0); init(); } private void OnEnable() { init(); } private void init() { ok.onClick.AddListener(MessageBox.onClickOk); cancel.onClick.AddListener(MessageBox.onClickCancel); retry.onClick.AddListener(MessageBox.onClickRetry); text.text = MessageBox.text; headText.text = MessageBox.headText; //根据传递的参数,进行样式的显示 switch (MessageBox.dialogResult) { case DialogResult.Ok: ok.gameObject.SetActive( true ); cancel.gameObject.SetActive( false ); retry.gameObject.SetActive( false ); break ; case DialogResult.OKCancel: ok.gameObject.SetActive( true ); cancel.gameObject.SetActive( true ); retry.gameObject.SetActive( false ); break ; case DialogResult.RetryCancel: ok.gameObject.SetActive( true ); cancel.gameObject.SetActive( true ); retry.gameObject.SetActive( true ); break ; case DialogResult.YesNo: ok.transform.GetChild(0).GetComponent<Text>().text = "是" ; cancel.transform.GetChild(0).GetComponent<Text>().text = "否" ; ok.gameObject.SetActive( true ); cancel.gameObject.SetActive( true ); retry.gameObject.SetActive( false ); break ; case DialogResult.YesNoCancel: ok.transform.GetChild(0).GetComponent<Text>().text = "是" ; cancel.transform.GetChild(0).GetComponent<Text>().text = "否" ; ok.gameObject.SetActive( true ); cancel.gameObject.SetActive( true ); retry.gameObject.SetActive( true ); break ; } } private void Update() { panel.SetActive(MessageBox.type); gameObject.SetActive(MessageBox.state); } } |
四、显示框的调用
此处调用可以自行设置一个按钮,在其点击事件中注册调用即可。
笔者使用项目中的方式进行演示。具体不做说明。调用方式已给出。
特别注意:由于UiMessage调用了MessageBox的方法,所以必须先初始化MessageBox的数据。使用什么就初始化什么。笔者使用了ok、cancel按钮(默认不初始化模式,即为模态框,不初始化DialogResult即为只显示ok按钮),所以注册了相应的点击事件(委托)。最后显示弹出框(整个包含遮罩和弹出框)。
五、运行结果
六、弹出框可拖拽移动
将DragManage添加到MessageBox物体上面。(如果你想让ui物体可拖拽,对其添加DragManage即可实现)
笔者就不做演示了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using UnityEngine; using UnityEngine.EventSystems; /// <summary> /// 只是用来处理拖拽 /// </summary> public class DragManage : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { private Vector3 offect; public void OnBeginDrag(PointerEventData eventData) { offect = Input.mousePosition - transform.position; } public void OnDrag(PointerEventData eventData) { transform.position = Input.mousePosition - offect; } public void OnEndDrag(PointerEventData eventData) { transform.position = Input.mousePosition - offect; } } |
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日