unity3d ,配合ngui做的一个弹窗系统

来源:互联网 发布:蚁群算法matlab程序 编辑:程序博客网 时间:2024/04/27 23:07

窗体有三种:几秒后消失的,按确定后消失的,选择是或者否然后消失的

消息传递用了一个事件系统

http://blog.csdn.net/abcdtty/article/details/13021237

一个比较简单高效的事件系统,
EventManager.Instance.AddListener<UINotificaionArgs>(CallNotification);//注册事件
//触发弹窗的方法
EventManager.Instance.Send(new UINotificationArgs(){NMessageType = NMessageType.AskForYesOrNot,del = 你要执行的方法 ,Infomation  = 你要显示的信息});//这个是带有按下确定后执行相应方法的弹窗
EventManager.Instance.Send(new UINotificationArgs(){NMessageType = NMessageType.AskForOk,Infomation  = 你要显示的信息});//这个是没有方法,但要你按下确认才会消失的弹窗
EventManager.Instance.Send(new UINotificationArgs(){NMessageType = NMessageType.NoAsk,Infomation  = 你要显示的信息,WaitTime = 4f});//这个是没有按钮,但是会在几秒后弹窗消失的模式


//理解了这个事件系统就容易理解原理了
//至于具体的NGUI实现,看下面的变量名称也大约能明白了,工程没法上传


using UnityEngine;using System.Collections;public class UINotificaionArgs : EventArgs {private NMessageType nMessageType;public NMessageType NMessageType {get {return nMessageType;}set {nMessageType = value;}}private string infomation;public string Infomation {get {return infomation;}set {infomation = value;}}private float  waitTime;public float WaitTime {get {return waitTime;}set {waitTime = value;}}//public  delegate void NotificationDel();public DelegateManage.NotificationDel del;}public  class DelegateManage{public  delegate void NotificationDel();}public enum NMessageType{NoAsk,AskForOk,AskForYesOrNot}


以上是事件的内容部分,传递三个参数,一个是弹窗的类型,一个是需要等待的时间(第一种窗体需要),一个按下确定后执行的方法的委托


下面的是弹窗的具体管理组件

首先注册事件

然后是根据传递来的事件的具体内容,执行相关的方法

最后的两个方法是为按钮准备的

按下yes之后执行pressOk()

就调用了通过事件传递的,按下确定后,需要执行的方法了

using UnityEngine;using System.Collections;public class NotifacationManager : MonoBehaviour {public UILabel  Info;public GameObject Ask;public GameObject AskOrNot;private NMessageType messageType;private float WaitTime;private DelegateManage.NotificationDel del;void Start(){EventManager.Instance.AddListener<UINotificaionArgs>(CallNotification);}public void DisplayWindow(){foreach (Transform child in this.transform) {child.gameObject.SetActive(true);}}public void HideWindow(){foreach (Transform child in this.transform) {child.gameObject.SetActive(false);}}private void CallNotification(UINotificaionArgs e){DisplayWindow ();Info.text = e.Infomation;switch (e.NMessageType) {case NMessageType.NoAsk:WaitTime = e.WaitTime;StartCoroutine(NoAsk());break;case NMessageType.AskForOk:del = e.del;AskForOk();break;case NMessageType.AskForYesOrNot:del = e.del;AskOkOrNot();break;}}IEnumerator  NoAsk(){Ask.SetActive (false);AskOrNot.SetActive (false);yield return new WaitForSeconds(WaitTime);HideWindow ();}void  AskForOk(){Ask.SetActive (true);AskOrNot.SetActive (false);}void  AskOkOrNot(){Ask.SetActive (false);AskOrNot.SetActive (true);}public void PressOK(){del ();HideWindow ();}public void PressNo(){HideWindow ();}}