Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- using System;
- using System.Threading.Tasks;
- public class MessageBox
- {
- public enum buttons
- {
- ok,
- cancel,
- }
- public readonly string Message;
- public readonly string Title;
- public readonly Action Callback;
- public readonly GameObject UIMessageBox;
- private MessageBox(string message, string title, Action callback, buttons button)
- {
- Message = message;
- Title = title;
- UIMessageBox = DisplayBox(InGameMenu.Instance.messageBoxPrefab);
- Callback = callback;
- SetText(button);
- }
- public static void Show(string errorMessage, string title)
- {
- new MessageBox(errorMessage, title, null, buttons.ok);
- }
- public static void Show(string errorMessage, string title, Action callback)
- {
- new MessageBox(errorMessage, title, callback, buttons.ok);
- }
- public static void Show(string errorMessage, string title, Action callback, buttons button)
- {
- new MessageBox(errorMessage, title, callback, button);
- }
- private GameObject DisplayBox(GameObject prefab)
- {
- return MonoBehaviour.Instantiate(prefab, InGameMenu.inGameMenuCanvas.transform);
- }
- private void SetText(buttons buttonText)
- {
- UIMessageBox.transform.GetChild(0).Find("Header").GetChild(0).GetComponent<TMP_Text>().text = Title;
- UIMessageBox.transform.GetChild(0).Find("Body").GetChild(0).GetComponent<TMP_Text>().text = Message;
- UIMessageBox.transform.GetChild(0).Find("Button").GetChild(0).GetComponent<TMP_Text>().text = buttonText.ToString();
- UIMessageBox.transform.GetChild(0).GetComponentInChildren<Button>().onClick.AddListener(delegate {
- MonoBehaviour.Destroy(UIMessageBox);
- if (Callback != null)
- {
- Callback.Invoke();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement