XRScrollRect.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using BeinLab.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using XRTool.UI;
  8. using XRTool.Util;
  9. namespace XRTool.WorldUI
  10. {
  11. /// <summary>
  12. /// LayoutGroup
  13. /// </summary>
  14. public enum LayoutType
  15. {
  16. None = -2,
  17. UnKnow = -1,
  18. GridLayoutGroup = 0,
  19. VerticalLayoutGroup = 1,
  20. HorizontalLayoutGroup = 2,
  21. }
  22. /// <summary>
  23. /// 目标:1实现基本列表功能,生成,销毁,刷新,点击事件的传递等,支持常规的Button,Toggle类型的组件支持
  24. /// 2,实现滑动循环列表
  25. /// 3,实现多项内容展示时的优化
  26. /// 4,指定距离停顿
  27. /// </summary>
  28. public class XRScrollRect : ScrollRect
  29. {
  30. public bool isLoop;
  31. public LayoutType layoutType = LayoutType.UnKnow;
  32. private int allCount;
  33. private int showCount;
  34. private Vector2 cellSize;
  35. private Vector2 cellSpace;
  36. //private bool isDraging = false;
  37. private ItemCell superCell;
  38. private LayoutGroup layoutGroup;
  39. private List<ItemCell> cellList;
  40. private Bounds viewBounds;
  41. //private float dragTime = 0.2f;
  42. //private float lastDragTime;
  43. protected override void Start()
  44. {
  45. base.Start();
  46. viewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
  47. }
  48. public LayoutGroup LayoutGroup
  49. {
  50. get
  51. {
  52. if (!layoutGroup)
  53. {
  54. layoutGroup = content.GetComponent<LayoutGroup>();
  55. }
  56. return layoutGroup;
  57. }
  58. }
  59. public List<ItemCell> CellList { get => cellList; set => cellList = value; }
  60. protected override void LateUpdate()
  61. {
  62. if ( velocity.magnitude > 66 || !IsMoveCalculatePosition())
  63. {
  64. base.LateUpdate();
  65. XRUpdateItemVisibility();
  66. }
  67. }
  68. /// <summary>
  69. /// 更新显示Scroll
  70. /// </summary>
  71. private void XRUpdateItemVisibility()
  72. {
  73. ///存在子物体,同时显示数据小于总物体数字时
  74. if (allCount > 0 && showCount < allCount && content.childCount > 0)
  75. {
  76. for (int i = 0; i < content.childCount; i++)
  77. {
  78. RectTransform child = content.GetChild(i) as RectTransform;
  79. if (child.anchoredPosition.x < 0)
  80. {
  81. child.GetChild(0).gameObject.SetActive(false);
  82. }
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// 计算最终落点
  88. /// </summary>
  89. /// <returns></returns>
  90. public bool IsMoveCalculatePosition()
  91. {
  92. return false;
  93. }
  94. /// <summary>
  95. /// 初始化数据
  96. /// </summary>
  97. /// <param name="count"></param>
  98. /// <param name="showCount"></param>
  99. /// <param name="cellSize"></param>
  100. /// <param name="cellSpace"></param>
  101. /// <param name="prefab"></param>
  102. public void InitData(int count, int showCount, Vector2 cellSize, Vector3 cellSpace, ItemCell prefab)
  103. {
  104. this.allCount = count;
  105. this.showCount = showCount;
  106. this.cellSize = cellSize;
  107. this.cellSpace = cellSpace;
  108. this.superCell = prefab;
  109. CreateScrollList();
  110. }
  111. public void ClearScrollList()
  112. {
  113. if (content)
  114. {
  115. for (int i = 0; i < content.childCount; i++)
  116. {
  117. Destroy(content.GetChild(i).gameObject);
  118. }
  119. }
  120. CellList.Clear();
  121. }
  122. /// <summary>
  123. /// 创建列表
  124. /// </summary>
  125. private void CreateScrollList(bool isClear = true)
  126. {
  127. if (CellList == null)
  128. {
  129. CellList = new List<ItemCell>();
  130. }
  131. if (isClear)
  132. {
  133. ClearScrollList();
  134. }
  135. GameObject prefab = superCell.GetInstance();
  136. ///prefab.GetInstance()获取抽象类的实例,不确定这么做可以正常运行,测试中
  137. if (prefab)
  138. {
  139. if (!isLoop)
  140. {
  141. int instanceCount = showCount;
  142. if (showCount >= allCount)
  143. {
  144. instanceCount = allCount;
  145. }
  146. for (int i = 0; i < instanceCount; i++)
  147. {
  148. GameObject obj = Instantiate(prefab);
  149. UnityUtil.SetParent(content, obj.transform);
  150. CellList.Add(obj.GetComponent<ItemCell>());
  151. }
  152. RectTransform body = GetComponent<RectTransform>();
  153. Vector2 size = body.sizeDelta;
  154. if (horizontal && !vertical)
  155. {
  156. size.y = cellSize.y;
  157. //size.x = instanceCount*cellSize.x +
  158. }
  159. else if (vertical && !horizontal)
  160. {
  161. size.x = cellSize.x;
  162. }
  163. }
  164. }
  165. if (LayoutGroup)
  166. {
  167. if (LayoutGroup is GridLayoutGroup)
  168. {
  169. var lay = (LayoutGroup as GridLayoutGroup);
  170. lay.cellSize = this.cellSize;
  171. lay.spacing = this.cellSpace;
  172. }
  173. else if (LayoutGroup is VerticalLayoutGroup)
  174. {
  175. var lay = (LayoutGroup as VerticalLayoutGroup);
  176. lay.spacing = this.cellSpace.y;
  177. }
  178. else if (LayoutGroup is HorizontalLayoutGroup)
  179. {
  180. var lay = (LayoutGroup as HorizontalLayoutGroup);
  181. lay.spacing = this.cellSpace.x;
  182. }
  183. }
  184. }
  185. //private void XRUpdateScrollbarVisibility()
  186. //{
  187. // if (horizontalScrollbar && !isShowHorizontal && horizontalScrollbar.isActiveAndEnabled)
  188. // {
  189. // horizontalScrollbar.gameObject.SetActive(false);
  190. // }
  191. // if (verticalScrollbar && !isShowVertical && verticalScrollbar.isActiveAndEnabled)
  192. // {
  193. // verticalScrollbar.gameObject.SetActive(false);
  194. // }
  195. //}
  196. public override void OnDrag(PointerEventData eventData)
  197. {
  198. base.OnDrag(eventData);
  199. }
  200. protected override void SetContentAnchoredPosition(Vector2 position)
  201. {
  202. base.SetContentAnchoredPosition(position);
  203. }
  204. //public override void OnBeginDrag(PointerEventData eventData)
  205. //{
  206. // base.OnBeginDrag(eventData);
  207. // //isDraging = true;
  208. // //lastDragTime = Time.time;
  209. //}
  210. //public override void OnEndDrag(PointerEventData eventData)
  211. //{
  212. // base.OnEndDrag(eventData);
  213. // //isDraging = false;
  214. // //if (Time.time - lastDragTime < dragTime)
  215. // //{
  216. // // if (eventData.pointerEnter)
  217. // // {
  218. // // var tmpbutton = eventData.pointerEnter.GetComponentInParent<IPointerClickHandler>();
  219. // // if (tmpbutton != null)
  220. // // {
  221. // // tmpbutton.OnPointerClick(eventData);
  222. // // }
  223. // // }
  224. // //}
  225. //}
  226. protected override void OnRectTransformDimensionsChange()
  227. {
  228. base.OnRectTransformDimensionsChange();
  229. viewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
  230. }
  231. private static float XRRubberDelta(float overStretching, float viewSize)
  232. {
  233. return (1 - (1 / ((Mathf.Abs(overStretching) * 0.55f / viewSize) + 1))) * viewSize * Mathf.Sign(overStretching);
  234. }
  235. private Vector2 XRCalculateOffset(Vector2 delta)
  236. {
  237. return XRInternalCalculateOffset(ref viewBounds, ref m_ContentBounds, horizontal, vertical, movementType, ref delta);
  238. }
  239. internal static Vector2 XRInternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, MovementType movementType, ref Vector2 delta)
  240. {
  241. Vector2 offset = Vector2.zero;
  242. if (movementType == MovementType.Unrestricted)
  243. return offset;
  244. Vector2 min = contentBounds.min;
  245. Vector2 max = contentBounds.max;
  246. // min/max offset extracted to check if approximately 0 and avoid recalculating layout every frame (case 1010178)
  247. if (horizontal)
  248. {
  249. min.x += delta.x;
  250. max.x += delta.x;
  251. float maxOffset = viewBounds.max.x - max.x;
  252. float minOffset = viewBounds.min.x - min.x;
  253. if (minOffset < -0.001f)
  254. offset.x = minOffset;
  255. else if (maxOffset > 0.001f)
  256. offset.x = maxOffset;
  257. }
  258. if (vertical)
  259. {
  260. min.y += delta.y;
  261. max.y += delta.y;
  262. float maxOffset = viewBounds.max.y - max.y;
  263. float minOffset = viewBounds.min.y - min.y;
  264. if (maxOffset > 0.001f)
  265. offset.y = maxOffset;
  266. else if (minOffset < -0.001f)
  267. offset.y = minOffset;
  268. }
  269. return offset;
  270. }
  271. }
  272. }