GenericDialog.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. cg.blocksRaycasts = true;
  44. cg.alpha = 1f;
  45. cg.interactable = true;
  46. }
  47. public void Hide() {
  48. cg.interactable = false;
  49. cg.alpha = 0f;
  50. cg.blocksRaycasts = false;
  51. }
  52. private static GenericDialog instance;
  53. public static GenericDialog Instance() {
  54. if (!instance) {
  55. instance = FindObjectOfType(typeof(GenericDialog)) as GenericDialog;
  56. if (!instance) {
  57. Debug.Log("There need to be at least one active GenericDialog on the screen");
  58. }
  59. }
  60. return instance;
  61. }
  62. }