CardExpanding3D.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /// <summary>
  2. /// Credit - ryanslikesocool
  3. /// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
  4. /// </summary>
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace UnityEngine.UI.Extensions
  10. {
  11. [ExecuteInEditMode]
  12. public class CardExpanding3D : MonoBehaviour
  13. {
  14. [SerializeField]
  15. private float lerpSpeed = 12;
  16. [SerializeField]
  17. private float cornerSize = 64;
  18. [Header("Parts")]
  19. public RectTransform[] cardCorners;
  20. public RectTransform[] cardEdges;
  21. public RectTransform cardCenter;
  22. [Header("Card Info")]
  23. [Tooltip("Positions and sizes card to its current transform.")]
  24. public bool cardAutoSize = true;
  25. public Vector2 cardSize;
  26. public Vector2 cardPosition;
  27. [Range(1, 96)]
  28. public int cardSuperness = 4;
  29. [Header("Page Info")]
  30. [Tooltip("Positions and sizes the page to the top third of the screen.")]
  31. public bool pageAutoSize = true;
  32. public Vector2 pageSize;
  33. public Vector2 pagePosition;
  34. [Range(1, 96)]
  35. public int pageSuperness = 96;
  36. ///Just like with the 2D version of this script, I don't recommend touching this.
  37. private int animationActive = 0;
  38. private Vector2[] nextCornerPos = new Vector2[4];
  39. private Vector2[] nextEdgePos = new Vector2[4];
  40. private Vector2[] nextEdgeScale = new Vector2[4];
  41. private Vector2 nextCenterScale;
  42. private Vector2 nextPos;
  43. private int nextSuperness;
  44. private RectTransform rect;
  45. private Vector2 nextMin;
  46. private Vector2 nextMax;
  47. void Start()
  48. {
  49. if (cardAutoSize)
  50. {
  51. cardSize = new Vector2(cardCorners[0].localScale.x * 2 + cardEdges[0].localScale.x, cardCorners[0].localScale.y * 2 + cardEdges[0].localScale.y);
  52. cardPosition = cardCenter.localPosition;
  53. }
  54. if (pageAutoSize)
  55. {
  56. pageSize = new Vector2(Screen.width, Screen.height / 3);
  57. pagePosition = new Vector2(0, Screen.height / 2 - pageSize.y / 2);
  58. }
  59. rect = GetComponent<RectTransform>();
  60. }
  61. void Update()
  62. {
  63. if (animationActive == 1 || animationActive == -1)
  64. {
  65. ///Lerps the corners to new positions and supernesses.
  66. for (int i = 0; i < cardCorners.Length; i++)
  67. {
  68. cardCorners[i].localPosition = Vector3.Lerp(cardCorners[i].localPosition, nextCornerPos[i], Time.deltaTime * lerpSpeed);
  69. cardCorners[i].GetComponent<SuperellipsePoints>().superness = Mathf.Lerp(cardCorners[i].GetComponent<SuperellipsePoints>().superness, nextSuperness, Time.deltaTime * lerpSpeed);
  70. ///Forces everything to either the card layout or the page layout once the superness is similar enough.
  71. if (Mathf.Abs(cardCorners[i].GetComponent<SuperellipsePoints>().superness - nextSuperness) <= 1)
  72. {
  73. cardCorners[i].localPosition = nextCornerPos[i];
  74. cardEdges[i].localPosition = nextEdgePos[i];
  75. cardEdges[i].localScale = new Vector3(nextEdgeScale[i].x, nextEdgeScale[i].y, 1);
  76. transform.localPosition = nextPos;
  77. cardCenter.localScale = new Vector3(nextCenterScale.x, nextCenterScale.y, 1);
  78. cardCorners[i].GetComponent<SuperellipsePoints>().superness = nextSuperness;
  79. rect.offsetMin = nextMin;
  80. rect.offsetMax = nextMax;
  81. }
  82. }
  83. ///Lerps the edges to new positions and sizes.
  84. for (int i = 0; i < cardEdges.Length; i++)
  85. {
  86. cardEdges[i].localPosition = Vector3.Lerp(cardEdges[i].localPosition, nextEdgePos[i], Time.deltaTime * lerpSpeed);
  87. cardEdges[i].localScale = Vector3.Lerp(cardEdges[i].localScale, new Vector3(nextEdgeScale[i].x, nextEdgeScale[i].y, 1), Time.deltaTime * lerpSpeed);
  88. }
  89. ///Lerps the center to new position and size.
  90. transform.localPosition = Vector3.Lerp(transform.localPosition, nextPos, Time.deltaTime * lerpSpeed);
  91. cardCenter.localScale = Vector3.Lerp(cardCenter.localScale, new Vector3(nextCenterScale.x, nextCenterScale.y, 1), Time.deltaTime * lerpSpeed);
  92. ///Lerps the RectTransform.
  93. rect.offsetMin = Vector3.Lerp(rect.offsetMin, nextMin, Time.deltaTime * lerpSpeed);
  94. rect.offsetMax = Vector3.Lerp(rect.offsetMax, nextMax, Time.deltaTime * lerpSpeed);
  95. }
  96. }
  97. public void ToggleCard()
  98. {
  99. if (animationActive != 1 || animationActive == 0)
  100. {
  101. animationActive = 1;
  102. ///Gets new corner positions.
  103. for (int i = 0; i < cardCorners.Length; i++)
  104. {
  105. float posX = pageSize.x / 2 * Mathf.Sign(cardCorners[i].localScale.x) - cardCorners[i].localScale.x;
  106. float posY = pageSize.y / 2 * Mathf.Sign(cardCorners[i].localScale.y) - cardCorners[i].localScale.y;
  107. nextCornerPos[i] = new Vector2(posX, posY);
  108. }
  109. ///Same concept as the last loop.
  110. for (int i = 0; i < cardEdges.Length; i++)
  111. {
  112. float posX = 0;
  113. float posY = 0;
  114. float scaleX = 0;
  115. float scaleY = 0;
  116. if (cardEdges[i].localPosition.x != 0)
  117. {
  118. posX = Mathf.Sign(cardEdges[i].localPosition.x) * ((pageSize.x / 2) - (cardEdges[i].localScale.x / 2));
  119. posY = 0;
  120. scaleX = cornerSize;
  121. scaleY = pageSize.y - cornerSize * 2;
  122. }
  123. else if (cardEdges[i].localPosition.y != 0)
  124. {
  125. posX = 0;
  126. posY = Mathf.Sign(cardEdges[i].localPosition.y) * ((pageSize.y / 2) - (cardEdges[i].localScale.y / 2));
  127. scaleX = pageSize.x - cornerSize * 2;
  128. scaleY = cornerSize;
  129. }
  130. nextEdgePos[i] = new Vector2(posX, posY);
  131. nextEdgeScale[i] = new Vector2(scaleX, scaleY);
  132. }
  133. nextCenterScale = pageSize - new Vector2(cornerSize * 2, cornerSize * 2);
  134. nextPos = pagePosition;
  135. nextSuperness = pageSuperness;
  136. nextMin = new Vector2(-pageSize.x / 2, -pageSize.y / 2) + nextPos;
  137. nextMax = new Vector2(pageSize.x / 2, pageSize.y / 2) + nextPos;
  138. }
  139. else if (animationActive != -1)
  140. {
  141. animationActive = -1;
  142. ///Gets new corner positions.
  143. for (int i = 0; i < cardCorners.Length; i++)
  144. {
  145. float posX = Mathf.Sign(cardCorners[i].localScale.x) * (cardSize.x / 2) - cardCorners[i].localScale.x;
  146. float posY = Mathf.Sign(cardCorners[i].localScale.y) * (cardSize.y / 2) - cardCorners[i].localScale.y;
  147. nextCornerPos[i] = new Vector2(posX, posY);
  148. }
  149. ///Same concept as the last loop.
  150. for (int i = 0; i < cardEdges.Length; i++)
  151. {
  152. float posX = 0;
  153. float posY = 0;
  154. float scaleX = 0;
  155. float scaleY = 0;
  156. if (cardEdges[i].localPosition.x != 0)
  157. {
  158. posX = Mathf.Sign(cardEdges[i].localPosition.x) * (cardSize.x / 2) - Mathf.Sign(cardEdges[i].localPosition.x) * (cardEdges[i].localScale.x / 2);
  159. posY = 0;
  160. scaleX = cornerSize;
  161. scaleY = cardSize.y - cornerSize * 2;
  162. }
  163. else if (cardEdges[i].localPosition.y != 0)
  164. {
  165. posX = 0;
  166. posY = Mathf.Sign(cardEdges[i].localPosition.y) * (cardSize.y / 2) - Mathf.Sign(cardEdges[i].localPosition.y) * (cardEdges[i].localScale.y / 2);
  167. scaleX = cardSize.x - cornerSize * 2;
  168. scaleY = cornerSize;
  169. }
  170. nextEdgePos[i] = new Vector2(posX, posY);
  171. nextEdgeScale[i] = new Vector2(scaleX, scaleY);
  172. }
  173. nextCenterScale = cardSize - new Vector2(cornerSize * 2, cornerSize * 2);
  174. nextPos = cardPosition;
  175. nextSuperness = cardSuperness;
  176. nextMin = new Vector2(-cardSize.x / 2, -cardSize.y / 2) + nextPos;
  177. nextMax = new Vector2(cardSize.x / 2, cardSize.y / 2) + nextPos;
  178. }
  179. }
  180. }
  181. }