關於MessageBox訊息彈出框的製作
阿新 • • 發佈:2019-01-07
廣告結束回來,精彩繼續!
我們遊戲中存在好多種訊息機制即二次確認框,這也是遊戲中非常重要的一部分,這可以給玩家帶來更好的體驗,而我們做遊戲的目標不就是給玩家創造美好的體驗麼?
一下是我在開發專案中開發的一個提示框元件即二次確認框:
Enum MessageBoxButtons
{
OK,
Cancel,
Sure
}
程式碼是很清晰了,至於不懂得,歡迎交流!using UnityEngine; using System; using System.Collections; // 對話提示框顯示元件 public class MessageBox : MonoBehaviour { public static MessageBox Instance { get { return instance; } } private static MessageBox instance = null; // 是否點選任意處關閉對話提示框 private bool isAnywhereQuit; private GameObject goOK; private GameObject goCancel; private GameObject goSure; private UILabel labelMessage; private UILabel labelOK; private UILabel labelCancel; private UILabel labelSure; <span style="white-space:pre"> </span>// 都是點選確認,取消還有空白處的回撥函式 private Action firstCallback; private Action secondCallback; private Action thirdCallback; private Action callback; private Action anyWhereCallback; void Awake() { instance = this; var refs = GetComponent<GameObjectRef>(); goOK = refs["OK"]; goCancel = refs["Cancel"]; goSure = refs["Sure"]; goOK.SetActive(false); goCancel.SetActive(false); goSure.SetActive(false); labelMessage = refs["Label_Message"].GetComponent<UILabel>(); labelOK = refs["Label_OK"].GetComponent<UILabel>(); labelCancel = refs["Label_Cancel"].GetComponent<UILabel>(); labelSure = refs["Label_Sure"].GetComponent<UILabel>(); } void OnDisable() { isAnywhereQuit = false; } public void Show(string message, MessageBoxButtons firstButton, Action firstCallback, MessageBoxButtons secondButton, Action secondCallback, Action callback, bool isAnywhereQuit = false) { Show(message, firstButton, firstCallback, secondButton, secondCallback, isAnywhereQuit); this.callback = callback; } // 顯示三個按鈕 public void Show(string message, string firstButtonText, Action firstCallback, string secondButtonText, Action secondCallback, string thirdButtonText, Action thirdCallback, bool isAnywhereQuit = false, Action anyWhereCallback = null) { showMessageBox(true); labelMessage.text = message; labelOK.text = firstButtonText; labelCancel.text = secondButtonText; labelSure.text = thirdButtonText; this.firstCallback = firstCallback; this.secondCallback = secondCallback; this.thirdCallback = thirdCallback; this.isAnywhereQuit = isAnywhereQuit; this.anyWhereCallback = anyWhereCallback; goOK.SetActive(true); goCancel.SetActive(true); goSure.SetActive(true); } // 顯示兩個按鈕 public void Show(string message, MessageBoxButtons firstButton, Action firstCallback, MessageBoxButtons secondButton, Action secondCallback, bool isAnywhereQuit = false, Action anyWhereCallback = null) { showMessageBox(true); labelMessage.text = message; showButtonLabelText(labelOK, firstButton); showButtonLabelText(labelCancel, secondButton); this.firstCallback = firstCallback; this.secondCallback = secondCallback; this.isAnywhereQuit = isAnywhereQuit; this.anyWhereCallback = anyWhereCallback; goOK.SetActive(true); goCancel.SetActive(true); goSure.SetActive(false); } // 只顯示一個按鈕 public void Show(string message, MessageBoxButtons button, Action callback, bool isAnywhereQuit = false) { showMessageBox(true); labelMessage.text = message; showButtonLabelText(labelSure, button); this.thirdCallback = callback; this.isAnywhereQuit = isAnywhereQuit; goOK.SetActive(false); goCancel.SetActive(false); goSure.SetActive(true); } public void ShowMessageBox(bool isActive) { showMessageBox(isActive); } public GameObject GetOKButton() { return goOK; } private void showMessageBox(bool isActive) { this.gameObject.SetActive(isActive); GUIResource.ToggleTooltipUICamera(isActive); } // 根據按鈕型別,顯示按鈕label上的文字 private void showButtonLabelText(UILabel label, MessageBoxButtons button) { switch(button) { case MessageBoxButtons.OK: label.text = StringTable.ST_OK; break; case MessageBoxButtons.Cancel: label.text = StringTable.ST_CANCEL; break; case MessageBoxButtons.Yes: label.text = StringTable.ST_YES; break; case MessageBoxButtons.No: label.text = StringTable.ST_NO; break; default: break; } } private void onClickOKButton() { showMessageBox(false); if (firstCallback != null) { firstCallback(); } if (callback != null) { callback(); callback = null; } } private void onClickCancelButton() { showMessageBox(false); if (secondCallback != null) { secondCallback(); } if (callback != null) { callback(); callback = null; } } private void onClickSureButton() { showMessageBox(false); if (thirdCallback != null) { thirdCallback(); thirdCallback = null; } if (callback != null) { callback(); callback = null; } } private void onClickMessageBox() { if (isAnywhereQuit) { showMessageBox(false); if (anyWhereCallback != null) { anyWhereCallback(); anyWhereCallback = null; } } } // end of class }
廣告之後再回來!