Confirm = 确定 Cancel = 取消 QuitConfirmTitle = 退出确认 QuitConfirmContent = 继续将退出游戏。\n确定退出?
using UnityEngine; using System.Collections; public class DontDestroyOnLoad : MonoBehaviour { // Use this for initialization void Start () { DontDestroyOnLoad(this.gameObject); } }
using UnityEngine; using System.Collections; public class KeyDetecter : MonoBehaviour { private CommonUIManager m_CommonUIManager = null; void Start() { m_CommonUIManager = Singleton.getInstance("CommonUIManager") as CommonUIManager; } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.Escape) && m_CommonUIManager != null) { m_CommonUIManager.ShowMessageBox( Localization.instance.Get("QuitConfirmTitle"), Localization.instance.Get("QuitConfirmContent"), MessageBox.Style.OKAndCancel, OnReceiveQuitConfirmResult); } } void OnReceiveQuitConfirmResult(MessageBox.Result result) { if (result == MessageBox.Result.OK) { Application.Quit(); } } }
using UnityEngine; using System.Collections; public class CommonUIDetail : MonoBehaviour { public TweenPosition messageBoxTween; public UILabel messageBoxTitle; public UILabel messageBoxContent; public GameObject[] buttonGroups = new GameObject[(int)MessageBox.Style.eNumCount]; public System.Action messageBoxConfirmCallback = null; public System.Action messageBoxCancelCallback = null; void OnConfirm() { if (messageBoxConfirmCallback != null) { messageBoxConfirmCallback(); } } void OnCancel() { if (messageBoxCancelCallback != null) { messageBoxCancelCallback(); } } }
using UnityEngine; using System.Collections; public class MessageBox { public delegate void OnReceiveMessageBoxResult(MessageBox.Result result); public enum Style { OnlyOK, OKAndCancel, eNumCount } public enum Result { OK, Cancel, eNumCount } } public class CommonUIManager : MonoBehaviour { public GameObject commonUIPrefab = null; public GameObject root; public TweenPosition messageBoxTween; public UILabel messageBoxTitle; public UILabel messageBoxContent; public GameObject[] buttonGroups = new GameObject[(int)MessageBox.Style.eNumCount]; private MessageBox.OnReceiveMessageBoxResult messageBoxCallback = null; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void ShowMessageBox(string title, string content, MessageBox.Style style, MessageBox.OnReceiveMessageBoxResult callback) { if (root == null) { commonUIPrefab = Resources.Load("CommonUIRoot") as GameObject; root = GameObject.Instantiate(commonUIPrefab) as GameObject; root.transform.parent = this.transform; CommonUIDetail uiDetail = root.GetComponent<CommonUIDetail>(); messageBoxTween = uiDetail.messageBoxTween; messageBoxTitle = uiDetail.messageBoxTitle; messageBoxContent = uiDetail.messageBoxContent; buttonGroups[(int)MessageBox.Style.OnlyOK] = uiDetail.buttonGroups[(int)MessageBox.Style.OnlyOK]; buttonGroups[(int)MessageBox.Style.OKAndCancel] = uiDetail.buttonGroups[(int)MessageBox.Style.OKAndCancel]; uiDetail.messageBoxConfirmCallback = OnConfirm; uiDetail.messageBoxCancelCallback = OnCancel; } messageBoxTitle.text = title; messageBoxContent.text = content; messageBoxCallback = callback; switch ((int)style) { case (int)MessageBox.Style.OnlyOK: buttonGroups[(int)MessageBox.Style.OnlyOK].SetActive(true); buttonGroups[(int)MessageBox.Style.OKAndCancel].SetActive(false); break; case (int)MessageBox.Style.OKAndCancel: buttonGroups[(int)MessageBox.Style.OnlyOK].SetActive(false); buttonGroups[(int)MessageBox.Style.OKAndCancel].SetActive(true); break; } messageBoxTween.Play(true); } void OnConfirm() { if (messageBoxCallback != null) { messageBoxCallback(MessageBox.Result.OK); messageBoxCallback = null; } messageBoxTween.Play(false); } void OnCancel() { if (messageBoxCallback != null) { messageBoxCallback(MessageBox.Result.Cancel); messageBoxCallback = null; } messageBoxTween.Play(false); } }
【Unity技巧】自定义消息框(弹出框),布布扣,bubuko.com
原文:http://blog.csdn.net/candycat1992/article/details/24984347