unity 弹窗提示_【原创】Unity3D 消息提示框
本文永久地址:http://www.omuying.com/article/60.aspx,【文章转载请注明出处!】
消息提示框,大家都并不陌生,他们的主要作用是给予用户提示,用 NGUI 来做消息提示框并不难,只是有些地方需要额外注意。
先来搭建测试环境,如图:
因为消息提示框只有用户触发之后都会消失,所以需要设置背景遮罩的属性,用来避免显示消息提示框的时候,用户还可以操作其他的行为,如图:
这儿需要注意的是设置按钮的 Anchors 属性,我们希望当面板显示多行文字的时候背景变大,但是按钮的整体位置不会变,变的只是与上边框的距离,所以我们需要设置,如图:
然后再设置背景框 Background 的 Anchors 属性,背景框的大小只会随着文本框的变化而变化,所以我们要把 Anchors 的 Target 设置为文本框(lblNote),如图:
面板设计好了,下面是代码部分,代码部分没有要注意的,简单、实用就好,消息提示框的相关代码如下(MessageBoxEnum.cs):
01using UnityEngine;
02using System.Collections;
03
04public class MessageBoxEnum
05{
06public delegate void OnReceiveMessageBoxResult(MessageBoxEnum.Result result);
07
08public enum Style
09{
10Ok,
11OkAndCancel
12}
13
14public enum Result
15{
16Ok,
17Cancel
18}
19}
MessageBoxPanel.cs
01using UnityEngine;
02using System.Collections;
03
04public class MessageBoxPanel : MonoBehaviour
05{
06public UILabel lblNote;
07public UIButton btnSubmit;
08public UIButton btnYes;
09public UIButton btnNo;
10
11private MessageBoxEnum.OnReceiveMessageBoxResult callback;
12
13void Awake()
14{
15UIEventListener.Get (this.btnSubmit.gameObject).onClick = OnSubmitHandler;
16UIEventListener.Get (this.btnYes.gameObject).onClick = OnSubmitHandler;
17UIEventListener.Get (this.btnNo.gameObject).onClick = OnCancelHandler;
18
19this.ResetButtons ();
20}
21
22private void ResetButtons()
23{
24NGUITools.SetActive (this.btnSubmit.gameObject,false);
25NGUITools.SetActive (this.btnYes.gameObject,false);
26NGUITools.SetActive (this.btnNo.gameObject,false);
27}
28
29public void ShowMessageBox(string context, MessageBoxEnum.Style style, MessageBoxEnum.OnReceiveMessageBoxResult callback)
30{
31this.lblNote.text = context;
32this.callback = callback;
33
34this.ResetButtons ();
35
36if (style == MessageBoxEnum.Style.Ok)
37{
38NGUITools.SetActive (this.btnSubmit.gameObject,true);
39}else if (style == MessageBoxEnum.Style.OkAndCancel) {
40NGUITools.SetActive (this.btnYes.gameObject,true);
41NGUITools.SetActive(this.btnNo.gameObject,true);
42}
43}
44
45private void OnSubmitHandler(GameObject o)
46{
47NGUITools.SetActive (this.gameObject,false);
48if (callback !=null)
49{
50callback.Invoke(MessageBoxEnum.Result.Ok);
51}
52}
53
54private void OnCancelHandler(GameObject o)
55{
56NGUITools.SetActive (this.gameObject,false);
57if (callback !=null)
58{
59callback.Invoke(MessageBoxEnum.Result.Cancel);
60}
61}
62}
MessageBoxManager.cs
01using UnityEngine;
02using System.Collections;
03
04public class MessageBoxManager
05{
06///
07/// 初始化弹出面板的父对象
08///
09/// User interface root.
10public static void Init(UIRoot uiRoot)
11{
12MessageBoxManager.uiRoot = uiRoot;
13}
14private static UIRoot uiRoot;
15private static MessageBoxPanel messageBoxPanel;
16
17///
18/// 显示消息提示框
19///
20/// Context.
21/// Style.
22/// Callback.
23public static void Show(string context, MessageBoxEnum.Style style, MessageBoxEnum.OnReceiveMessageBoxResult callback)
24{
25if (messageBoxPanel ==null)
26{
27GameObject gameObject = Resources.Load("MessageBoxPanel");
28if(gameObject !=null)
29{
30GameObject messageBoxObject = (GameObject)GameObject.Instantiate(gameObject);
31if(messageBoxObject !=null)
32{
33messageBoxObject.transform.parent = MessageBoxManager.uiRoot.transform;
34messageBoxObject.transform.localPosition =new Vector3(0f, 0f, 0f);
35messageBoxObject.transform.localScale =new Vector3(1f, 1f, 1f);
36
37messageBoxPanel = messageBoxObject.GetComponent();
38}
39}
40}
41if(messageBoxPanel !=null)
42{
43NGUITools.SetActive(messageBoxPanel.gameObject,true);
44messageBoxPanel.ShowMessageBox(context, style, callback);
45}
46}
47}
然后把 MessageBoxPanel.cs 挂载到 MessageBoxPanel 对象上面,如图:
接着把 MessageBoxPanel 对象设置成预设,以方便以后实例化,如图:
下面我们建议一个测试面板,如图:
新建立一个 C# 脚本,取名:DemoPanel,代码如下:
01using UnityEngine;
02using System.Collections;
03
04public class DemoPanel : MonoBehaviour
05{
06public UIRoot uiRoot;
07public UIButton btnOne;
08public UIButton btnMore;
09
10void Awake()
11{
12MessageBoxManager.Init (uiRoot);
13
14UIEventListener.Get (this.btnOne.gameObject).onClick = OnOneClickHandler;
15UIEventListener.Get (this.btnMore.gameObject).onClick = OnMoreClickHandler;
16}
17
18private void OnOneClickHandler(GameObject o)
19{
20MessageBoxManager.Show ("这是单行文本提示框", MessageBoxEnum.Style.Ok, OnOneCallback);
21}
22
23private void OnMoreClickHandler(GameObject o)
24{
25MessageBoxManager.Show ("这是多行文本提示框\n显示“是”和“否”按钮!", MessageBoxEnum.Style.OkAndCancel, OnMoreCallback);
26}
27
28private void OnOneCallback(MessageBoxEnum.Result result)
29{
30Debug.Log ("单行文本提示框回调函数");
31}
32
33private void OnMoreCallback(MessageBoxEnum.Result result)
34{
35if(result == MessageBoxEnum.Result.Ok)
36{
37Debug.Log("您点击了是按钮!");
38}else{
39Debug.Log("您点击了否按钮!");
40}
41}
42}
然后给 DemoPanel 对象挂载 DemoPanel.cs 脚本,如图:
运行游戏,最终效果如下:
资源下载地址:点击下载,共下载 55 次。
unity 弹窗提示_【原创】Unity3D 消息提示框相关推荐
- php 消息对话框弹出,php弹窗代码_php弹出消息提示框的二种方法
摘要 腾兴网为您分享:php弹出消息提示框的二种方法,优衣库,新浪博客,万师傅,顺丰等软件知识,以及符号,甘肃农商银行,彗星小,kindle人社区,广东省公务用车管理,宝鸡在线,火影忍者英文版,寂静岭 ...
- mysql代码提示_聊一聊 php 代码提示
title: 聊一聊 php 代码提示 date: 2017-8-25 15:05:49 这次我们来聊一聊 php 的代码提示, 不使用 IDE 的同学也可以瞧瞧看, PHP IDE 推荐 phpst ...
- 关闭切换大小写提示_你不知道的iOS 13提示和技巧
iOS 13充满了我们过去几年一直在询问的新功能和变化.在黑暗的模式终于在这里,提醒应用程序要好得多,快捷键的应用程序现在有真正的自动化,Safari浏览器得到一个下载管理器和文件应用最后用外部存储工 ...
- 安装了silverlight还是提示_苹果手机的微信提示音也能改,公司小姐姐都来向我请教...
我帮小伙伴们回忆一下,啊哆~啊哆~啊哆~,想起来了吗?没想起来就往下看待会儿就能听见了. 下面就教大家怎么制作自己的微信提示音. 重中之重:系统版本是iOS12-iOS12.12版本之间! 1. 首先 ...
- ssms2008 代码自动提示_使用 SSMS 的提示和技巧 - SQL Server Management Studio (SSMS) | Microsoft Docs...
使用 SQL Server Management Studio (SSMS) 的提示和技巧Tips and tricks for using SQL Server Management Studio ...
- mac 终端提示_有用的终端提示
mac 终端提示 A while ago I started a thread on Twitter with a few terminal tips. There are lots of comma ...
- java 摸拟qq消息提示_java 仿qq消息提示框
引用包: swt.jar 主类: import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import ...
- html5载入提示音,html5新消息提示声音
[实例简介] [实例截图] [核心代码] HTML5手机声音提示 #chatBox{width:400px;border:1px solid #d3d3d3;margin:50px auto;} #c ...
- unity 发光字体_[转载]Unity3D做彩色的字体的详细教学
效果是这样的: 1-1.jpeg (15.73 KiB) 被浏览 24 次 详细的实现方法: 1 首先随便扔进去一个字体,最简单的比如扔进去一个arial. 设置好Font Size,比如24 Cha ...
最新文章
- 【Linux_Fedora_应用系列】_3_如何利用Smplayer播放WMV格式的文件
- 美国北卡教堂山分校Jeff Dangl组植物微生物组博士后招聘(植物微生物互作领域第一高引学者)...
- wget抓取数据,需要用户登录验证
- mysql socket错误_解决Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’错误...
- 基于边缘的图像分割——分水岭算法(watershed)算法分析(附opencv源码分析)
- 【有三吐槽】程序员改BUG的六重境界,你,第几重了?
- 2021华宁三五班高考成绩查询,2021娄底市地区高考成绩排名查询
- php查询字段的总和,ThinkPHP 多表查询-如其字段A相同,则把字段B相加
- mybatis的typeAliases别名
- NVRH.265:鱼与熊掌不兼得?不见得!
- 常用php操作redis命令整理(五)ZSET类型
- hdu 1078 FatMouse and Cheese(记忆化搜索)
- 计算机考研数据结构参考书,2020考研计算机备考:数据结构参考书及重点
- 安卓ps模拟器_用电脑玩安卓游戏!逍遥模拟器免安装版PC软件
- 建设银行查看完整卡号
- 如何用python爬取图片数据_python爬虫怎么实现爬取网站图片?
- ubuntu关闭网络防火墙后,外网依然不能连接ubuntu的端口
- Android仿微信拍摄、录制视频,以及视频播放(基于JCameraView和GSYVideoPlayer)
- 基于MindSpore复现Deeplabv3—语义分割
- 存储卡中各种文件夹用处