YesNoObject.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. public class YesNoObject : MonoBehaviour
  8. {
  9. public Text InfoText;
  10. public List<CommandButton> Buttons;
  11. public Animator anim;
  12. public GameObject CloseButton;
  13. //public Vector3 defaultPos = Vector3.zero;
  14. public bool CloseOnButtonClicked = true;
  15. public void SetUpYesNoObject(List<UnityAction> OnClickActions, List<string> CommandTexts, string _InfoText, bool CanClose)
  16. {
  17. //this.transform.localPosition = defaultPos;
  18. for (int i = 0; i < Buttons.Count; i++)
  19. {
  20. Buttons[i].OnClickAction = null;
  21. if (i < OnClickActions.Count)
  22. {
  23. Buttons[i].gameObject.SetActive(true);
  24. Buttons[i].transform.GetChild(0).GetComponent<Text>().text = CommandTexts[i];
  25. int k = i;
  26. Buttons[i].OnClickAction = () =>
  27. {
  28. if (!Buttons[k].GetComponent<Button>().interactable)
  29. return;
  30. OnClickActions[k]?.Invoke();
  31. if(this.CloseOnButtonClicked)
  32. {
  33. this.Close_(false);
  34. }
  35. };
  36. }
  37. else
  38. {
  39. Buttons[i].gameObject.SetActive(false);
  40. }
  41. }
  42. InfoText.text = _InfoText;
  43. this.gameObject.SetActive(true);
  44. CloseButton.SetActive(CanClose);
  45. Open();
  46. }
  47. public void Off()
  48. {
  49. this.gameObject.SetActive(false);
  50. Close_(false);
  51. }
  52. public void Open()
  53. {
  54. this.gameObject.SetActive(true);
  55. anim.SetInteger("Open", 1);
  56. anim.SetInteger("Close", 0);
  57. }
  58. public void Close()
  59. {
  60. Close_(true);
  61. }
  62. public void Close_(bool playSE)
  63. {
  64. if (playSE)
  65. {
  66. Opening.instance.PlayCancelSE();
  67. }
  68. anim.SetInteger("Open", 0);
  69. anim.SetInteger("Close", 1);
  70. }
  71. }