本文实例为大家分享了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; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/unknown6248/p/11784813.html