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. }
  22. for (int i = 0; i < Buttons.Count; i++)
  23. {
  24. if (i < OnClickActions.Count)
  25. {
  26. Buttons[i].gameObject.SetActive(true);
  27. Buttons[i].transform.GetChild(0).GetComponent<Text>().text = CommandTexts[i];
  28. int k = i;
  29. Buttons[i].OnClickAction = () =>
  30. {
  31. OnClickActions[k]?.Invoke();
  32. if(this.CloseOnButtonClicked)
  33. {
  34. this.Close_(false);
  35. }
  36. };
  37. }
  38. else
  39. {
  40. Buttons[i].gameObject.SetActive(false);
  41. }
  42. }
  43. InfoText.text = _InfoText;
  44. this.gameObject.SetActive(true);
  45. CloseButton.SetActive(CanClose);
  46. Open();
  47. }
  48. public void Off()
  49. {
  50. this.gameObject.SetActive(false);
  51. Close_(false);
  52. }
  53. public void Open()
  54. {
  55. this.gameObject.SetActive(true);
  56. anim.SetInteger("Open", 1);
  57. anim.SetInteger("Close", 0);
  58. }
  59. public void Close()
  60. {
  61. Close_(true);
  62. }
  63. public void Close_(bool playSE)
  64. {
  65. if (playSE)
  66. {
  67. Opening.instance.PlayCancelSE();
  68. }
  69. anim.SetInteger("Open", 0);
  70. anim.SetInteger("Close", 1);
  71. }
  72. }