CommandPanel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using UnityEngine.Events;
  7. [System.Serializable]
  8. public class CommandPanel : MonoBehaviour
  9. {
  10. [Header("コマンドパネルオブジェクト")]
  11. [SerializeField] GameObject CommandPanelObject;
  12. [Header("コマンドパネル背景")]
  13. [SerializeField] RectTransform CommandPanelBackGround;
  14. [Header("ボタン親")]
  15. [SerializeField] Transform buttonParent;
  16. bool first = false;
  17. private void Start()
  18. {
  19. if (!false)
  20. {
  21. first = true;
  22. #if !UNITY_EDITOR && UNITY_ANDROID
  23. CommandPanelObject.transform.localScale *= 1.5f;
  24. #endif
  25. }
  26. }
  27. public bool isActive()
  28. {
  29. return CommandPanelObject.gameObject.activeSelf;
  30. }
  31. public void SetUpCommandPanel(List<CardCommand> CardCommands, FieldPermanentCard fieldPokemonCard, HandCard handCard)
  32. {
  33. if (CardCommands.Count == 0)
  34. {
  35. return;
  36. }
  37. List<CardCommandButton> Buttons = new List<CardCommandButton>();
  38. for (int i = 0; i < buttonParent.childCount; i++)
  39. {
  40. Buttons.Add(new CardCommandButton(buttonParent.GetChild(i).GetComponent<Button>(), buttonParent.GetChild(i).GetChild(0).GetComponent<TextMeshProUGUI>()));
  41. }
  42. for (int i = 0; i < Buttons.Count; i++)
  43. {
  44. Buttons[i].CloseFieldUnitCommandButton();
  45. }
  46. for (int i = 0; i < Buttons.Count; i++)
  47. {
  48. if (i < CardCommands.Count)
  49. {
  50. Buttons[i].SetUpFieldUnitCommandButton(CardCommands[i].ButtonMessage, CardCommands[i].OnClickAction, CardCommands[i].Active, CardCommands[i].color);
  51. }
  52. else
  53. {
  54. break;
  55. }
  56. }
  57. //CommandPanelBackGround.sizeDelta = new Vector2(CommandPanelBackGround.sizeDelta.x, 4.6f * (FieldUnitCommands.Count + 1));
  58. GridLayoutGroup grid = CommandPanelObject.transform.GetChild(1).GetComponent<GridLayoutGroup>();
  59. float defaultWidth = 241.42f;
  60. int constraint = CardCommands.Count + 1;
  61. if (grid != null)
  62. {
  63. constraint = grid.constraintCount;
  64. }
  65. CommandPanelBackGround.sizeDelta = new Vector2(defaultWidth * (((CardCommands.Count - 1) / constraint) + 1), 27.07f * (CardCommands.Count - 1) + 104.63f);
  66. if (fieldPokemonCard != null)
  67. {
  68. if (fieldPokemonCard.ThisPermanent != null)
  69. {
  70. if (fieldPokemonCard.ThisPermanent.IsSuspended)
  71. {
  72. CommandPanelObject.transform.localRotation = Quaternion.Euler(0, 0, 90);
  73. }
  74. else
  75. {
  76. CommandPanelObject.transform.localRotation = Quaternion.Euler(0, 0, 180);
  77. }
  78. }
  79. }
  80. else if (handCard != null)
  81. {
  82. CommandPanelObject.transform.localRotation = Quaternion.Euler(0, 0, 180);
  83. }
  84. if (handCard != null)
  85. {
  86. if (handCard.cardSource != null)
  87. {
  88. CommandPanelObject.transform.localPosition = new Vector2(0f, 31f);
  89. }
  90. }
  91. CommandPanelObject.SetActive(true);
  92. }
  93. public void CloseCommandPanel()
  94. {
  95. if (CommandPanelObject != null)
  96. {
  97. CommandPanelObject.SetActive(false);
  98. }
  99. }
  100. }
  101. #region コマンド
  102. public class CardCommand
  103. {
  104. public CardCommand(string ButtonMessage, UnityAction OnClickAction, bool Active, Color color)
  105. {
  106. this.ButtonMessage = ButtonMessage;
  107. this.OnClickAction = OnClickAction;
  108. this.Active = Active;
  109. this.color = color;
  110. }
  111. public string ButtonMessage { get; set; }
  112. public UnityAction OnClickAction { get; set; }
  113. public bool Active { get; set; }
  114. public Color color { get; set; }
  115. }
  116. #endregion
  117. #region コマンドボタン
  118. public class CardCommandButton
  119. {
  120. public Button button;
  121. public TextMeshProUGUI ButtonText;
  122. public CardCommandButton(Button button, TextMeshProUGUI ButtonText)
  123. {
  124. this.button = button;
  125. this.ButtonText = ButtonText;
  126. }
  127. public void SetUpFieldUnitCommandButton(string ButtonMessage, UnityAction OnClickAtion, bool Active, Color color)
  128. {
  129. ButtonText.text = ButtonMessage;
  130. button.gameObject.SetActive(true);
  131. button.interactable = Active;
  132. button.image.color = color;
  133. button.onClick.RemoveAllListeners();
  134. if (Active)
  135. {
  136. button.onClick.AddListener(() => { OnClickAtion?.Invoke(); });
  137. }
  138. }
  139. public void CloseFieldUnitCommandButton()
  140. {
  141. button.gameObject.SetActive(false);
  142. }
  143. }
  144. #endregion