LoopVerticalScrollRect.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using UnityEngine.EventSystems;
  5. namespace UnityEngine.UI
  6. {
  7. [AddComponentMenu("UI/Loop Vertical Scroll Rect", 51)]
  8. [DisallowMultipleComponent]
  9. public class LoopVerticalScrollRect : LoopScrollRect
  10. {
  11. bool bInited = false;
  12. SlideGridLayoutGroup slideLayout;
  13. protected override float GetSize(RectTransform item)
  14. {
  15. float size = contentSpacing;
  16. if (m_GridLayout != null)
  17. {
  18. size += m_GridLayout.cellSize.y;
  19. }
  20. else
  21. {
  22. size += LayoutUtility.GetPreferredHeight(item);
  23. }
  24. return size;
  25. }
  26. protected override float GetDimension(Vector2 vector)
  27. {
  28. return vector.y;
  29. }
  30. protected override Vector2 GetVector(float value)
  31. {
  32. return new Vector2(0, value);
  33. }
  34. protected override void Awake()
  35. {
  36. base.Awake();
  37. directionSign = -1;
  38. GridLayoutGroup layout = content.GetComponent<GridLayoutGroup>();
  39. if (layout != null && layout.constraint != GridLayoutGroup.Constraint.FixedColumnCount)
  40. {
  41. //Debug.LogError("[LoopHorizontalScrollRect] unsupported GridLayoutGroup constraint");
  42. }
  43. if (layout != null && (layout.startCorner == GridLayoutGroup.Corner.LowerLeft || layout.startCorner == GridLayoutGroup.Corner.LowerRight))
  44. {
  45. lowGrid = true;
  46. }
  47. if (layout == null)
  48. {
  49. slideLayout = content.GetComponent<SlideGridLayoutGroup>();
  50. if (slideLayout != null && slideLayout.constraint != SlideGridLayoutGroup.Constraint.FixedColumnCount)
  51. {
  52. //Debug.LogError("[LoopHorizontalScrollRect] unsupported GridLayoutGroup constraint");
  53. }
  54. if (slideLayout != null && (slideLayout.startCorner == SlideGridLayoutGroup.Corner.LowerLeft || slideLayout.startCorner == SlideGridLayoutGroup.Corner.LowerRight))
  55. {
  56. lowGrid = true;
  57. }
  58. }
  59. if (layout == null)
  60. {
  61. SlideVerticalLayoutGroup slideLayout1 = content.GetComponent<SlideVerticalLayoutGroup>();
  62. if (slideLayout1 != null && slideLayout1.inverseChildPosEnable)
  63. {
  64. lowGrid = true;
  65. }
  66. }
  67. }
  68. protected override bool UpdateItems(Bounds viewBounds, Bounds contentBounds)
  69. {
  70. if (!bInited)
  71. {
  72. bInited = true;
  73. CalcContentCount(viewBounds);
  74. }
  75. bool changed = false;
  76. if (viewBounds.min.y < contentBounds.min.y + 1)
  77. {
  78. float size = lowGrid? NewItemAtStart() : NewItemAtEnd();
  79. if (size > 0)
  80. {
  81. if (threshold < size)
  82. {
  83. threshold = size * 1.1f;
  84. }
  85. changed = true;
  86. }
  87. }
  88. else if (viewBounds.min.y > contentBounds.min.y + threshold)
  89. {
  90. float size = lowGrid? DeleteItemAtStart() : DeleteItemAtEnd();
  91. if (size > 0)
  92. {
  93. changed = true;
  94. }
  95. }
  96. if (viewBounds.max.y > contentBounds.max.y - 1)
  97. {
  98. float size = lowGrid ? NewItemAtEnd() : NewItemAtStart();
  99. if (size > 0)
  100. {
  101. if (threshold < size)
  102. {
  103. threshold = size * 1.1f;
  104. }
  105. changed = true;
  106. }
  107. }
  108. else if (viewBounds.max.y < contentBounds.max.y - threshold)
  109. {
  110. float size = lowGrid ? DeleteItemAtEnd() : DeleteItemAtStart();
  111. if (size > 0)
  112. {
  113. changed = true;
  114. }
  115. }
  116. return changed;
  117. }
  118. void CalcContentCount(Bounds viewBounds)
  119. {
  120. if (mContentCount == 0)
  121. {
  122. LayoutElement elm = Cell.GetComponent<LayoutElement>();
  123. if(elm.preferredHeight > 0)
  124. {
  125. mContentCount = Mathf.CeilToInt(viewBounds.size.y / (elm.preferredHeight + contentSpacing));
  126. }else if(elm.minHeight > 0)
  127. {
  128. mContentCount = Mathf.CeilToInt(viewBounds.size.y / (elm.minHeight + contentSpacing));
  129. }else if(elm.flexibleHeight > 0)
  130. {
  131. mContentCount = Mathf.CeilToInt(viewBounds.size.y / (elm.flexibleHeight + contentSpacing));
  132. }
  133. }
  134. }
  135. public override void OnBeginDrag(PointerEventData eventData)
  136. {
  137. base.OnBeginDrag(eventData);
  138. LoopScrollRectDragEventInherit srInhert = GetComponent<LoopScrollRectDragEventInherit>();
  139. if (srInhert == null) return;
  140. ScrollRect sr = srInhert.GetInheritScrollRect();
  141. if (sr == null) return;
  142. if (sr.vertical)
  143. {
  144. sr.OnBeginDrag(eventData);
  145. }
  146. }
  147. public override void OnDrag(PointerEventData eventData)
  148. {
  149. base.OnDrag(eventData);
  150. LoopScrollRectDragEventInherit srInhert = GetComponent<LoopScrollRectDragEventInherit>();
  151. if (srInhert == null) return;
  152. ScrollRect sr = srInhert.GetInheritScrollRect();
  153. if (sr == null) return;
  154. if (sr.vertical)
  155. {
  156. sr.OnDrag(eventData);
  157. }
  158. }
  159. public override void OnEndDrag(PointerEventData eventData)
  160. {
  161. base.OnEndDrag(eventData);
  162. LoopScrollRectDragEventInherit srInhert = GetComponent<LoopScrollRectDragEventInherit>();
  163. if (srInhert == null) return;
  164. ScrollRect sr = srInhert.GetInheritScrollRect();
  165. if (sr == null) return;
  166. if (sr.vertical)
  167. {
  168. sr.OnEndDrag(eventData);
  169. }
  170. }
  171. }
  172. }