GenericDialog.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. [RequireComponent(typeof(CanvasGroup))]
  7. public class GenericDialog : MonoBehaviour {
  8. public Text title;
  9. public Text message;
  10. public Text accept, decline;
  11. public Button acceptButton, declineButton;
  12. private CanvasGroup cg;
  13. void Awake() {
  14. cg = GetComponent<CanvasGroup>();
  15. }
  16. public GenericDialog SetOnAccept(string text, UnityAction action) {
  17. accept.text = text;
  18. acceptButton.onClick.RemoveAllListeners();
  19. acceptButton.onClick.AddListener(action);
  20. return this;
  21. }
  22. public GenericDialog SetOnDecline(string text, UnityAction action) {
  23. if (text.Equals("")) {
  24. declineButton.gameObject.SetActive(false);
  25. } else {
  26. declineButton.gameObject.SetActive(true);
  27. decline.text = text;
  28. declineButton.onClick.RemoveAllListeners();
  29. declineButton.onClick.AddListener(action);
  30. }
  31. return this;
  32. }
  33. public GenericDialog SetTitle(string title) {
  34. this.title.text = title;
  35. return this;
  36. }
  37. public GenericDialog SetMessage(string message) {
  38. this.message.text = message;
  39. return this;
  40. }
  41. public void Show() {
  42. this.transform.SetAsLastSibling();
  43. Transform iconPanel = GameObject.Find("IconPanel").GetComponent<Transform>();
  44. if (iconPanel.childCount == 0) {
  45. RectTransform rt = iconPanel.GetComponent<RectTransform>();
  46. rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
  47. RectTransform rtd = GameObject.Find("DialogTitle").GetComponent<RectTransform>();
  48. rtd.offsetMin = new Vector2(0, rtd.offsetMin.y);
  49. }
  50. cg.blocksRaycasts = false;
  51. cg.alpha = 1f;
  52. cg.interactable = true;
  53. this.GetComponentInChildren<GenericDialog>().GetComponent<CanvasGroup>().blocksRaycasts = true;
  54. }
  55. public void Hide() {
  56. cg.interactable = false;
  57. cg.alpha = 0f;
  58. cg.blocksRaycasts = false;
  59. }
  60. private static GenericDialog instance;
  61. public static GenericDialog Instance() {
  62. if (!instance) {
  63. instance = FindObjectOfType(typeof(GenericDialog)) as GenericDialog;
  64. if (!instance) {
  65. Debug.Log("There need to be at least one active GenericDialog on the screen");
  66. }
  67. }
  68. return instance;
  69. }
  70. }