SelectCommandPanel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class SelectCommandPanel : MonoBehaviour
  6. {
  7. [Header("Command Selection Button Prefab")]
  8. public SelectCommand selectCommandPrefab;
  9. private void Start()
  10. {
  11. oldCommandTextPos = GManager.instance.commandText.transform.localPosition;
  12. }
  13. #region Open command selection panel
  14. public Coroutine SetUpCommandButton(List<Command_SelectCommand> commands)
  15. {
  16. if (this.gameObject.activeSelf)
  17. {
  18. return null;
  19. }
  20. this.transform.parent.gameObject.SetActive(true);
  21. this.gameObject.SetActive(true);
  22. GetComponent<Animator>().SetInteger("Close", 0);
  23. return StartCoroutine(SetUpCommandButtonCoroutine(commands));
  24. }
  25. Vector3 oldCommandTextPos = Vector3.zero;
  26. IEnumerator SetUpCommandButtonCoroutine(List<Command_SelectCommand> commands)
  27. {
  28. oldCommandTextPos = GManager.instance.commandText.transform.localPosition;
  29. if (commands.Count >= 2)
  30. {
  31. //GManager.instance.commandText.transform.localPosition += new Vector3(0, 86 * (commands.Count - 1));
  32. GManager.instance.commandText.transform.localPosition += new Vector3(0, 90 * (commands.Count - 1));
  33. }
  34. for (int i = 0; i < transform.childCount; i++)
  35. {
  36. Destroy(transform.GetChild(i).gameObject);
  37. }
  38. yield return new WaitWhile(() => transform.childCount > 0);
  39. if (commands != null)
  40. {
  41. GManager.instance.sideBar.SetUpSideBar();
  42. commands.Reverse();
  43. foreach (Command_SelectCommand command in commands)
  44. {
  45. SelectCommand _selectCommandButton = Instantiate(selectCommandPrefab, transform);
  46. _selectCommandButton.OpenSelectCommandButton(command.CommandName, command.Command, command.SpriteIndex);
  47. _selectCommandButton.OnClickEvent.AddListener(() => { for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(false); } });
  48. _selectCommandButton.OnClickEvent.AddListener(CloseSelectCommandPanel);
  49. }
  50. if (commands.Count == 0)
  51. {
  52. Off();
  53. }
  54. }
  55. else
  56. {
  57. Off();
  58. }
  59. }
  60. #endregion
  61. #region Close command selection panel
  62. public void CloseSelectCommandPanel()
  63. {
  64. if (!this.gameObject.activeSelf)
  65. {
  66. return;
  67. }
  68. GetComponent<Animator>().SetInteger("Close", 1);
  69. }
  70. bool first = false;
  71. public void Off()
  72. {
  73. Off(true);
  74. }
  75. public void Off(bool returnDefaultPos)
  76. {
  77. this.gameObject.SetActive(false);
  78. GManager.instance.sideBar.OffSideBar(returnDefaultPos);
  79. if (first)
  80. {
  81. GManager.instance.commandText.transform.localPosition = oldCommandTextPos;
  82. }
  83. first = true;
  84. }
  85. #endregion
  86. }
  87. public class Command_SelectCommand
  88. {
  89. public string CommandName { get; set; }
  90. public UnityAction Command { get; set; }
  91. public int SpriteIndex { get; set; }
  92. public Command_SelectCommand(string _CommandName, UnityAction _Command, int _SpriteIndex)
  93. {
  94. CommandName = _CommandName;
  95. Command = _Command;
  96. SpriteIndex = _SpriteIndex;
  97. }
  98. }