CardStack2D.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// <summary>
  2. /// Credit - ryanslikesocool
  3. /// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
  4. /// </summary>
  5. using System.Collections;
  6. namespace UnityEngine.UI.Extensions
  7. {
  8. public class CardStack2D : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private float cardMoveSpeed = 8f;
  12. [SerializeField]
  13. private float buttonCooldownTime = 0.125f;
  14. [SerializeField]
  15. private int cardZMultiplier = 32;
  16. [SerializeField]
  17. private bool useDefaultUsedXPos = true;
  18. [SerializeField]
  19. private int usedCardXPos = 1280;
  20. [SerializeField]
  21. private KeyCode leftButton = KeyCode.LeftArrow;
  22. [SerializeField]
  23. private KeyCode rightButton = KeyCode.RightArrow;
  24. [SerializeField]
  25. private Transform[] cards = null;
  26. private int cardArrayOffset;
  27. private Vector3[] cardPositions;
  28. private int xPowerDifference;
  29. ///Static variables can be used across the scene if this script is in it.
  30. ///Thankfully it doesn't matter if another script attempts to use the variable and this script isn't in the scene.
  31. public static bool canUseHorizontalAxis = true;
  32. void Start()
  33. {
  34. ///I've found that 9 is a good number for this.
  35. ///I wouldn't really recommend changing it, but go ahead if you want to.
  36. xPowerDifference = 9 - cards.Length;
  37. ///This is optional, but makes it super easy to figure out the off screen position for cards.
  38. ///Unfortunately, it's only really useful if the cards are the same width.
  39. if (useDefaultUsedXPos)
  40. {
  41. int cardWidth = (int)(cards[0].GetComponent<RectTransform>().rect.width);
  42. usedCardXPos = (int)(Screen.width * 0.5f + cardWidth);
  43. }
  44. cardPositions = new Vector3[cards.Length * 2 - 1];
  45. ///This loop is for cards still in the stack.
  46. for (int i = cards.Length; i > -1; i--)
  47. {
  48. if (i < cards.Length - 1)
  49. {
  50. cardPositions[i] = new Vector3(-Mathf.Pow(2, i + xPowerDifference) + cardPositions[i + 1].x, 0, cardZMultiplier * Mathf.Abs(i + 1 - cards.Length));
  51. }
  52. else
  53. {
  54. cardPositions[i] = Vector3.zero;
  55. }
  56. }
  57. ///This loop is for cards outside of the stack.
  58. for (int i = cards.Length; i < cardPositions.Length; i++)
  59. {
  60. cardPositions[i] = new Vector3(usedCardXPos + 4 * (i - cards.Length), 0, -2 + -2 * (i - cards.Length));
  61. }
  62. }
  63. void Update()
  64. {
  65. if (canUseHorizontalAxis)
  66. {
  67. ///Controls for the cards.
  68. if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") < 0 || UIExtensionsInputManager.GetKey(leftButton)) && cardArrayOffset > 0)
  69. {
  70. cardArrayOffset--;
  71. StartCoroutine(ButtonCooldown());
  72. }
  73. else if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") > 0 || UIExtensionsInputManager.GetKey(rightButton)) && cardArrayOffset < cards.Length - 1)
  74. {
  75. cardArrayOffset++;
  76. StartCoroutine(ButtonCooldown());
  77. }
  78. }
  79. ///This loop moves the cards. I know that none of my lerps are the "right way," but it looks much nicer.
  80. for (int i = 0; i < cards.Length; i++)
  81. {
  82. cards[i].localPosition = Vector3.Lerp(cards[i].localPosition, cardPositions[i + cardArrayOffset], Time.deltaTime * cardMoveSpeed);
  83. if (Mathf.Abs(cards[i].localPosition.x - cardPositions[i + cardArrayOffset].x) < 0.01f)
  84. {
  85. cards[i].localPosition = cardPositions[i + cardArrayOffset];
  86. ///This disables interaction with cards that are not on top of the stack.
  87. if (cards[i].localPosition.x == 0)
  88. {
  89. cards[i].gameObject.GetComponent<CanvasGroup>().interactable = true;
  90. }
  91. else
  92. {
  93. cards[i].gameObject.GetComponent<CanvasGroup>().interactable = false;
  94. }
  95. }
  96. }
  97. }
  98. ///Stops the cards from scrolling super quickly if a button on the horizontal axis is held down.
  99. IEnumerator ButtonCooldown()
  100. {
  101. canUseHorizontalAxis = false;
  102. yield return new WaitForSeconds(buttonCooldownTime);
  103. canUseHorizontalAxis = true;
  104. }
  105. }
  106. }