LoopVerticalScrollRectMulti.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(MultiPrefab)", 53)]
  8. [DisallowMultipleComponent]
  9. public class LoopVerticalScrollRectMulti : LoopScrollRectMulti
  10. {
  11. LoopVerticalScrollRectMulti()
  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. if (viewBounds.min.y < contentBounds.min.y + m_ContentBottomPadding)
  109. {
  110. float size = await NewItemAtEnd(), totalSize = size;
  111. while (size > 0 && viewBounds.min.y < contentBounds.min.y + m_ContentBottomPadding - totalSize)
  112. {
  113. size = await NewItemAtEnd();
  114. totalSize += size;
  115. }
  116. if (totalSize > 0)
  117. changed = true;
  118. }
  119. if (viewBounds.max.y > contentBounds.max.y - m_ContentTopPadding)
  120. {
  121. float size = await NewItemAtStart(), totalSize = size;
  122. while (size > 0 && viewBounds.max.y > contentBounds.max.y - m_ContentTopPadding + totalSize)
  123. {
  124. size = await NewItemAtStart();
  125. totalSize += size;
  126. }
  127. if (totalSize > 0)
  128. changed = true;
  129. }
  130. if (viewBounds.min.y > contentBounds.min.y + threshold + m_ContentBottomPadding)
  131. {
  132. float size = DeleteItemAtEnd(), totalSize = size;
  133. while (size > 0 && viewBounds.min.y > contentBounds.min.y + threshold + m_ContentBottomPadding + totalSize)
  134. {
  135. size = DeleteItemAtEnd();
  136. totalSize += size;
  137. }
  138. if (totalSize > 0)
  139. changed = true;
  140. }
  141. if (viewBounds.max.y < contentBounds.max.y - threshold - m_ContentTopPadding)
  142. {
  143. float size = DeleteItemAtStart(), totalSize = size;
  144. while (size > 0 && viewBounds.max.y < contentBounds.max.y - threshold - m_ContentTopPadding - totalSize)
  145. {
  146. size = DeleteItemAtStart();
  147. totalSize += size;
  148. }
  149. if (totalSize > 0)
  150. changed = true;
  151. }
  152. if (changed)
  153. {
  154. ClearTempPool();
  155. }
  156. return (changed,viewBounds,contentBounds);
  157. }
  158. }
  159. }*/