GenericDialog.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. decline.text = text;
  24. declineButton.onClick.RemoveAllListeners();
  25. declineButton.onClick.AddListener(action);
  26. return this;
  27. }
  28. public GenericDialog SetTitle(string title) {
  29. this.title.text = title;
  30. return this;
  31. }
  32. public GenericDialog SetMessage(string message) {
  33. this.message.text = message;
  34. return this;
  35. }
  36. public void Show() {
  37. this.transform.SetAsLastSibling();
  38. cg.blocksRaycasts = true;
  39. cg.alpha = 1f;
  40. cg.interactable = true;
  41. }
  42. public void Hide() {
  43. cg.interactable = false;
  44. cg.alpha = 0f;
  45. cg.blocksRaycasts = false;
  46. }
  47. private static GenericDialog instance;
  48. public static GenericDialog Instance() {
  49. if (!instance) {
  50. instance = FindObjectOfType(typeof(GenericDialog)) as GenericDialog;
  51. if (!instance) {
  52. Debug.Log("There need to be at least one active GenericDialog on the screen");
  53. }
  54. }
  55. return instance;
  56. }
  57. }