CustomVerticleScrollView.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using SC.XR.Unity.Module_InputSystem;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace GHZLangChao
  8. {
  9. public class CustomVerticleScrollView : MonoBehaviour, IPointerDownHandler
  10. {
  11. public List<string> data = new List<string>();
  12. public RectTransform content;
  13. public List<RoomName_Item> itemList = new List<RoomName_Item>();
  14. private Vector3 lastPosition;
  15. private float offset;
  16. public int startIndex = 0;
  17. public int endIndex = 0;
  18. private float size;
  19. private float index0Y;
  20. private bool isMoveItem; // 标记当前的Item是否需要挪动
  21. private bool sliderUp;
  22. private bool isDrag;
  23. public float moveSpeed = 2000;
  24. private void Awake()
  25. {
  26. for (int i = 0; i < 20; i++) // 添加10个数据
  27. {
  28. data.Add("4A-"+i.ToString()+"机房");
  29. }
  30. //data = MachineRoomManager.Instance.listRoomName;
  31. size = itemList[0].GetComponent<RectTransform>().rect.size.y;
  32. index0Y = itemList[0].transform.localPosition.y;
  33. for (int i = 0; i < itemList.Count; i++) // 显示几个数据(这里为5个)
  34. {
  35. itemList[i].GetComponentInChildren<Text>().text = data[i];
  36. }
  37. startIndex = 0;
  38. endIndex = itemList.Count - 2;
  39. DispatcherBase.KeyDownDelegateRegister(downEvent);
  40. }
  41. private void downEvent(InputKeyCode keyCode, InputDevicePartBase part)
  42. {
  43. if (part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null)
  44. {
  45. checkMoveDown(part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject, part);
  46. }
  47. }
  48. public void checkMoveDown(GameObject obj, InputDevicePartBase part)
  49. {
  50. if (obj)
  51. {
  52. if (obj.name==this.gameObject.name)
  53. {
  54. Debug.Log("OnPointerDown");
  55. isDrag = true;
  56. lastPosition = part.inputDataBase.SCPointEventData.position;
  57. }
  58. else
  59. {
  60. if (obj.transform.parent != null)
  61. checkMoveDown(obj.transform.parent.gameObject, part);
  62. }
  63. }
  64. }
  65. private void UpdateList(Vector2 position)
  66. {
  67. offset = position.y - lastPosition.y;
  68. offset = Mathf.Clamp(offset, -75, 75); // Item高/2
  69. lastPosition = position;
  70. for (int i = 0; i < itemList.Count; i++)
  71. {
  72. itemList[i].transform.localPosition += Vector3.up * offset;
  73. }
  74. if (offset > 0) // 往上滑动
  75. {
  76. if (!sliderUp && isMoveItem)
  77. {
  78. startIndex++;
  79. if (startIndex > data.Count - 1)
  80. startIndex = 0;
  81. isMoveItem = false;
  82. }
  83. sliderUp = true;
  84. if (itemList[0].transform.localPosition.y >= index0Y && !isMoveItem)
  85. {
  86. isMoveItem = true;
  87. Debug.Log("添加一个Item到末尾");
  88. itemList[itemList.Count - 1].transform.localPosition = itemList[itemList.Count - 2].transform.localPosition - Vector3.up * size;
  89. endIndex++;
  90. if (endIndex > data.Count - 1)
  91. endIndex = 0;
  92. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = data[endIndex];
  93. }
  94. else
  95. {
  96. if (itemList[0].transform.localPosition.y >= index0Y + size)
  97. {
  98. isMoveItem = false;
  99. Debug.Log("将第一个Item放到末尾");
  100. RoomName_Item temp = itemList[0];
  101. temp.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition - Vector3.up * size;
  102. for (int i = 1; i < itemList.Count; i++)
  103. {
  104. itemList[i - 1] = itemList[i];
  105. }
  106. itemList[itemList.Count - 1] = temp;
  107. startIndex++;
  108. if (startIndex > data.Count - 1)
  109. startIndex = 0;
  110. }
  111. }
  112. }
  113. else if (offset < 0)// 往下滑动
  114. {
  115. if (sliderUp && isMoveItem)
  116. {
  117. endIndex--;
  118. if (endIndex < 0)
  119. endIndex = data.Count - 1;
  120. isMoveItem = false;
  121. }
  122. sliderUp = false;
  123. if (itemList[0].transform.localPosition.y <= index0Y && !isMoveItem)
  124. {
  125. isMoveItem = true;
  126. Debug.Log("添加一个Item到顶部");
  127. itemList[itemList.Count - 1].transform.localPosition = itemList[0].transform.localPosition + Vector3.up * size;
  128. startIndex--;
  129. if (startIndex < 0)
  130. startIndex = data.Count - 1;
  131. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = data[startIndex];
  132. }
  133. else
  134. {
  135. if (itemList[0].transform.localPosition.y <= index0Y - size)
  136. {
  137. Debug.Log("将最后一个Item放到顶部");
  138. RoomName_Item temp = itemList[itemList.Count - 1];
  139. temp.transform.localPosition = itemList[0].transform.localPosition + Vector3.up * size;
  140. isMoveItem = false;
  141. for (int i = itemList.Count - 1; i >= 1; i--)
  142. {
  143. itemList[i] = itemList[i - 1];
  144. }
  145. itemList[0] = temp;
  146. endIndex--;
  147. if (endIndex < 0)
  148. endIndex = data.Count - 1;
  149. }
  150. }
  151. }
  152. }
  153. private Vector3 targetPos;
  154. private Vector3 currentPos;
  155. private void Update()
  156. {
  157. if (isDrag)
  158. {
  159. if (Input.GetMouseButtonUp(0))
  160. {
  161. isDrag = false;
  162. targetPos = Input.mousePosition + Vector3.Project(Input.mousePosition - currentPos, Vector3.up) * moveSpeed;
  163. }
  164. else
  165. {
  166. currentPos = Input.mousePosition;
  167. UpdateList(currentPos);
  168. }
  169. }
  170. else
  171. {
  172. currentPos = Vector3.Lerp(currentPos, targetPos, Time.deltaTime);
  173. UpdateList(currentPos);
  174. }
  175. }
  176. public void OnPointerDown(PointerEventData eventData)
  177. {
  178. isDrag = true;
  179. lastPosition = eventData.position;
  180. }
  181. }
  182. }