LoopVerticalScrollRect.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using ET;
  5. namespace UnityEngine.UI
  6. {
  7. //[AddComponentMenu("UI/Loop Vertical Scroll Rect", 51)]
  8. [DisallowMultipleComponent]
  9. public class LoopVerticalScrollRect : LoopScrollRect
  10. {
  11. LoopVerticalScrollRect()
  12. {
  13. direction = LoopScrollRectDirection.Vertical;
  14. }
  15. protected override float GetSize(RectTransform item, bool includeSpacing)
  16. {
  17. float size = includeSpacing ? contentSpacing : 0;
  18. if (m_GridLayout != null)
  19. {
  20. size += m_GridLayout.cellSize.y;
  21. }
  22. else
  23. {
  24. size += LayoutUtility.GetPreferredHeight(item);
  25. }
  26. size *= m_Content.localScale.y;
  27. return size;
  28. }
  29. protected override float GetDimension(Vector2 vector)
  30. {
  31. return vector.y;
  32. }
  33. protected override float GetAbsDimension(Vector2 vector)
  34. {
  35. return vector.y;
  36. }
  37. protected override Vector2 GetVector(float value)
  38. {
  39. return new Vector2(0, value);
  40. }
  41. protected override void Awake()
  42. {
  43. base.Awake();
  44. if (m_Content)
  45. {
  46. GridLayoutGroup layout = m_Content.GetComponent<GridLayoutGroup>();
  47. if (layout != null && layout.constraint != GridLayoutGroup.Constraint.FixedColumnCount)
  48. {
  49. Debug.LogError($"[LoopScrollRect] {this.gameObject.name} 不支持的GridLayoutGroup约束 必须使用 FixedColumnCount",this);
  50. }
  51. }
  52. }
  53. protected override async ETTask<(bool,Bounds,Bounds)> UpdateItems(Bounds viewBounds, Bounds contentBounds)
  54. {
  55. bool changed = false;
  56. // special case: handling move several page in one frame
  57. if ((viewBounds.size.y < contentBounds.min.y - viewBounds.max.y) && itemTypeEnd > itemTypeStart)
  58. {
  59. int maxItemTypeStart = -1;
  60. if (totalCount >= 0)
  61. {
  62. maxItemTypeStart = Mathf.Max(0, totalCount - (itemTypeEnd - itemTypeStart));
  63. }
  64. float currentSize = contentBounds.size.y;
  65. float elementSize = (currentSize - contentSpacing * (CurrentLines - 1)) / CurrentLines;
  66. ReturnToTempPool(true, itemTypeEnd - itemTypeStart);
  67. itemTypeStart = itemTypeEnd;
  68. int offsetCount = Mathf.FloorToInt((contentBounds.min.y - viewBounds.max.y) / (elementSize + contentSpacing));
  69. if (maxItemTypeStart >= 0 && itemTypeStart + offsetCount * contentConstraintCount > maxItemTypeStart)
  70. {
  71. offsetCount = Mathf.FloorToInt((float)(maxItemTypeStart - itemTypeStart) / contentConstraintCount);
  72. }
  73. itemTypeStart += offsetCount * contentConstraintCount;
  74. if (totalCount >= 0)
  75. {
  76. itemTypeStart = Mathf.Max(itemTypeStart, 0);
  77. }
  78. itemTypeEnd = itemTypeStart;
  79. float offset = offsetCount * (elementSize + contentSpacing);
  80. m_Content.anchoredPosition -= new Vector2(0, offset + (reverseDirection ? 0 : currentSize));
  81. contentBounds.center -= new Vector3(0, offset + currentSize / 2, 0);
  82. contentBounds.size = Vector3.zero;
  83. changed = true;
  84. }
  85. if ((viewBounds.min.y - contentBounds.max.y > viewBounds.size.y) && itemTypeEnd > itemTypeStart)
  86. {
  87. float currentSize = contentBounds.size.y;
  88. float elementSize = (currentSize - contentSpacing * (CurrentLines - 1)) / CurrentLines;
  89. ReturnToTempPool(false, itemTypeEnd - itemTypeStart);
  90. itemTypeEnd = itemTypeStart;
  91. int offsetCount = Mathf.FloorToInt((viewBounds.min.y - contentBounds.max.y) / (elementSize + contentSpacing));
  92. if (totalCount >= 0 && itemTypeStart - offsetCount * contentConstraintCount < 0)
  93. {
  94. offsetCount = Mathf.FloorToInt((float)(itemTypeStart) / contentConstraintCount);
  95. }
  96. itemTypeStart -= offsetCount * contentConstraintCount;
  97. if (totalCount >= 0)
  98. {
  99. itemTypeStart = Mathf.Max(itemTypeStart, 0);
  100. }
  101. itemTypeEnd = itemTypeStart;
  102. float offset = offsetCount * (elementSize + contentSpacing);
  103. m_Content.anchoredPosition += new Vector2(0, offset + (reverseDirection ? currentSize : 0));
  104. contentBounds.center += new Vector3(0, offset + currentSize / 2, 0);
  105. contentBounds.size = Vector3.zero;
  106. changed = true;
  107. }
  108. // issue #149: new item before delete
  109. if (viewBounds.min.y < contentBounds.min.y + m_ContentBottomPadding)
  110. {
  111. float size = await NewItemAtEnd(), totalSize = size;
  112. while (size > 0 && viewBounds.min.y < contentBounds.min.y + m_ContentBottomPadding - totalSize)
  113. {
  114. size = await NewItemAtEnd();
  115. totalSize += size;
  116. }
  117. if (totalSize > 0)
  118. changed = true;
  119. }
  120. if (viewBounds.max.y > contentBounds.max.y - m_ContentTopPadding)
  121. {
  122. float size = await NewItemAtStart(), totalSize = size;
  123. while (size > 0 && viewBounds.max.y > contentBounds.max.y - m_ContentTopPadding + totalSize)
  124. {
  125. size = await NewItemAtStart();
  126. totalSize += size;
  127. }
  128. if (totalSize > 0)
  129. changed = true;
  130. }
  131. if (viewBounds.min.y > contentBounds.min.y + threshold + m_ContentBottomPadding)
  132. {
  133. float size = DeleteItemAtEnd(), totalSize = size;
  134. while (size > 0 && viewBounds.min.y > contentBounds.min.y + threshold + m_ContentBottomPadding + totalSize)
  135. {
  136. size = DeleteItemAtEnd();
  137. totalSize += size;
  138. }
  139. if (totalSize > 0)
  140. changed = true;
  141. }
  142. if (viewBounds.max.y < contentBounds.max.y - threshold - m_ContentTopPadding)
  143. {
  144. float size = DeleteItemAtStart(), totalSize = size;
  145. while (size > 0 && viewBounds.max.y < contentBounds.max.y - threshold - m_ContentTopPadding - totalSize)
  146. {
  147. size = DeleteItemAtStart();
  148. totalSize += size;
  149. }
  150. if (totalSize > 0)
  151. changed = true;
  152. }
  153. if (changed)
  154. {
  155. ClearTempPool();
  156. }
  157. return (changed,viewBounds,contentBounds);
  158. }
  159. }
  160. }