TestList.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. public class TestList : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
  7. {
  8. bool _isPress;
  9. Vector2 _curPos;
  10. // item列表
  11. List<Transform> _items;
  12. float _height;
  13. // 首尾位置
  14. float _start_y = 0f;
  15. float _end_y = 0f;
  16. // 移动总距离
  17. float _moveDis = 0;
  18. // 列表数据
  19. public List<int> _data;
  20. // 间距
  21. public float _interval = 5f;
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. // 获取所有item,重复GetChild很浪费内存
  26. _items = new List<Transform>();
  27. for (int i = 0; i < transform.childCount; i++)
  28. {
  29. _items.Add(transform.GetChild(i));
  30. }
  31. // 自定义列表数据
  32. _data = new List<int>();
  33. for (int i = 0; i < 1000; i++)
  34. {
  35. _data.Add(i);
  36. }
  37. _height = _items[0].GetComponent<RectTransform>().sizeDelta.y;
  38. _start_y = _items[0].GetComponent<RectTransform>().localPosition.y;
  39. _end_y = _items[_items.Count - 1].GetComponent<RectTransform>().localPosition.y;
  40. _moveDis = _height * -1f;
  41. initList();
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. if (_isPress) moveItems();
  47. }
  48. #if UNITY_EDITOR
  49. private void OnValidate()
  50. {
  51. if (_interval != transform.GetComponent<VerticalLayoutGroup>().spacing) resetInterval();
  52. }
  53. #endif
  54. public void OnPointerClick(PointerEventData eventData)
  55. {
  56. _isPress = false;
  57. }
  58. public void OnPointerDown(PointerEventData eventData)
  59. {
  60. _isPress = true;
  61. _curPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  62. }
  63. public void OnPointerUp(PointerEventData eventData)
  64. {
  65. _isPress = false;
  66. _curPos = Vector2.zero;
  67. }
  68. // 编辑器状态下重置item之间的间隔
  69. void resetInterval()
  70. {
  71. transform.GetComponent<VerticalLayoutGroup>().enabled = true;
  72. transform.GetComponent<VerticalLayoutGroup>().spacing = _interval;
  73. transform.GetComponent<VerticalLayoutGroup>().enabled = false;
  74. }
  75. // 初始化列表数据
  76. void initList()
  77. {
  78. for (int i = 0; i < _items.Count; i++)
  79. {
  80. _items[i].GetChild(0).GetComponent<Text>().text = _data[i].ToString();
  81. }
  82. }
  83. void moveItems()
  84. {
  85. float _y = Input.mousePosition.y - _curPos.y;
  86. _curPos.y = Input.mousePosition.y;
  87. for (int i = 0; i < _items.Count; i++)
  88. {
  89. _items[i].localPosition = new Vector3(_items[i].localPosition.x, _items[i].localPosition.y + _y, 0);
  90. }
  91. // item位置移到最后
  92. if (_items[0].localPosition.y >= (_start_y + _height))
  93. {
  94. _items[0].localPosition = new Vector3(_items[_items.Count - 1].localPosition.x, _items[_items.Count - 1].localPosition.y - _height - _interval, 0);
  95. Transform item_temp = _items[0];
  96. _items.RemoveAt(0);
  97. _items.Add(item_temp);
  98. }
  99. // item位置移到最上
  100. if (_items[_items.Count - 1].localPosition.y <= _end_y)
  101. {
  102. _items[_items.Count - 1].localPosition = new Vector3(_items[0].localPosition.x, _items[0].localPosition.y + _height + _interval, 0);
  103. Transform item_temp = _items[_items.Count - 1];
  104. _items.RemoveAt(_items.Count - 1);
  105. _items.Insert(0, item_temp);
  106. }
  107. _moveDis += _y;
  108. refeshScroll();
  109. }
  110. int _index_data;
  111. void refeshScroll()
  112. {
  113. if (_moveDis >= (_height + _interval))
  114. {
  115. // 向上移动时
  116. _moveDis = 0f;
  117. _index_data++;
  118. if (_index_data > _data.Count - 1) _index_data = 0;
  119. }
  120. if (_moveDis <= (_height + _interval) * -1)
  121. {
  122. // 向下移动时
  123. _moveDis = 0f;
  124. _index_data--;
  125. if (_index_data < 0) _index_data = _data.Count - 1;
  126. }
  127. if (_moveDis == 0)
  128. {
  129. for (int i = 0; i < _items.Count; i++)
  130. {
  131. initItem(_items[i], _index_data + i);
  132. }
  133. }
  134. }
  135. // 刷新item中的数据
  136. void initItem(Transform item, int index)
  137. {
  138. if (index >= _data.Count) index -= _data.Count;
  139. if (index < 0) index += _data.Count;
  140. // Debug.Log("???" + index);
  141. item.GetChild(0).GetComponent<Text>().text = _data[index].ToString();
  142. }
  143. }