CustomHorizintalScrollView.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. public class CustomHorizintalScrollView : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  6. {
  7. private List<SceneValue> mSceneData;
  8. public RectTransform content;
  9. public List<SChooseItem> itemList = new List<SChooseItem>();
  10. private Vector3 lastPosition;
  11. private float offset;
  12. public int startIndex = 0;
  13. public int endIndex = 0;
  14. private float size;
  15. private float index0Y;
  16. private bool isMoveItem; // 标记当前的Item是否需要挪动
  17. private bool sliderUp;
  18. private bool isDrag;
  19. private float speed = 20; // 拖动速度
  20. public void Init(List<SceneValue> m_SceneData, SceneChoose SceneChoose)
  21. {
  22. mSceneData = m_SceneData;
  23. size = itemList[0].GetComponent<RectTransform>().rect.size.x;
  24. index0Y = itemList[0].transform.localPosition.x;
  25. if (m_SceneData.Count == 1)
  26. {
  27. itemList[2].gameObject.SetActive(false);
  28. itemList.Remove(itemList[2]);
  29. itemList[1].gameObject.SetActive(false);
  30. itemList.Remove(itemList[1]);
  31. Vector3 temp = itemList[0].gameObject.GetComponent<RectTransform>().localPosition;
  32. temp.x = 600;
  33. itemList[0].gameObject.GetComponent<RectTransform>().localPosition = temp;
  34. }
  35. else if (m_SceneData.Count == 2)
  36. {
  37. itemList[2].gameObject.SetActive(false);
  38. itemList.Remove(itemList[2]);
  39. }
  40. for (int i = 0; i < itemList.Count; i++) // 显示几个数据(这里为5个)
  41. {
  42. itemList[i].GetComponentInChildren<Text>().text = mSceneData[i].name;
  43. itemList[i].SceneData = mSceneData[i];
  44. }
  45. startIndex = 0;
  46. endIndex = itemList.Count - 2;
  47. }
  48. private void UpdateList(Vector2 position)
  49. {
  50. offset = position.x - lastPosition.x;
  51. offset = Mathf.Clamp(offset, -50, 50); // Item宽/2
  52. lastPosition = position;
  53. for (int i = 0; i < itemList.Count; i++)
  54. {
  55. itemList[i].transform.localPosition += Vector3.right * offset;
  56. }
  57. if (offset < 0) // 往左滑动
  58. {
  59. if (!sliderUp && isMoveItem)
  60. {
  61. startIndex++;
  62. if (startIndex > mSceneData.Count - 1)
  63. startIndex = 0;
  64. isMoveItem = false;
  65. }
  66. sliderUp = true;
  67. if (itemList[0].transform.localPosition.x <= index0Y && !isMoveItem)
  68. {
  69. isMoveItem = true;
  70. Debug.Log("添加一个Item到末尾");
  71. itemList[itemList.Count - 1].transform.localPosition = itemList[itemList.Count - 2].transform.localPosition + Vector3.right * size;
  72. endIndex++;
  73. if (endIndex > mSceneData.Count - 1)
  74. endIndex = 0;
  75. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[endIndex].name;
  76. itemList[itemList.Count - 1].SceneData = mSceneData[endIndex];
  77. }
  78. else
  79. {
  80. if (itemList[0].transform.localPosition.x <= index0Y - size)
  81. {
  82. isMoveItem = false;
  83. Debug.Log("将第一个Item放到末尾");
  84. SChooseItem temp = itemList[0];
  85. temp.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition + Vector3.right * size;
  86. for (int i = 1; i < itemList.Count; i++)
  87. {
  88. itemList[i - 1] = itemList[i];
  89. }
  90. itemList[itemList.Count - 1] = temp;
  91. startIndex++;
  92. if (startIndex > mSceneData.Count - 1)
  93. startIndex = 0;
  94. }
  95. }
  96. }
  97. else if (offset > 0)// 往右滑动
  98. {
  99. if (sliderUp && isMoveItem)
  100. {
  101. endIndex--;
  102. if (endIndex < 0)
  103. endIndex = mSceneData.Count - 1;
  104. isMoveItem = false;
  105. }
  106. sliderUp = false;
  107. if (itemList[0].transform.localPosition.x >= index0Y && !isMoveItem)
  108. {
  109. isMoveItem = true;
  110. Debug.Log("添加一个Item到顶部");
  111. itemList[itemList.Count - 1].transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
  112. startIndex--;
  113. if (startIndex < 0)
  114. startIndex = mSceneData.Count - 1;
  115. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[startIndex].name;
  116. itemList[itemList.Count - 1].SceneData = mSceneData[startIndex];
  117. }
  118. else
  119. {
  120. if (itemList[0].transform.localPosition.x >= size - index0Y)
  121. {
  122. Debug.Log("将最后一个Item放到顶部");
  123. SChooseItem temp = itemList[itemList.Count - 1];
  124. temp.transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
  125. isMoveItem = false;
  126. for (int i = itemList.Count - 1; i >= 1; i--)
  127. {
  128. itemList[i] = itemList[i - 1];
  129. }
  130. itemList[0] = temp;
  131. endIndex--;
  132. if (endIndex < 0)
  133. endIndex = mSceneData.Count - 1;
  134. }
  135. }
  136. }
  137. }
  138. private Vector3 targetPos;
  139. private Vector3 currentPos;
  140. public void OnBeginDrag(PointerEventData eventData)
  141. {
  142. if (itemList.Count < 3) return;
  143. if ((eventData.button == PointerEventData.InputButton.Left))
  144. {
  145. isDrag = true;
  146. lastPosition = eventData.position;
  147. }
  148. }
  149. public void OnEndDrag(PointerEventData eventData)
  150. {
  151. if (itemList.Count < 3) return;
  152. isDrag = false;
  153. targetPos = eventData.pointerPressRaycast.worldPosition + Vector3.Project(eventData.pointerPressRaycast.worldPosition - currentPos, Vector3.right) * speed;
  154. //targetPos = Input.mousePosition + Vector3.Project(Input.mousePosition - currentPos, Vector3.right) * speed;
  155. }
  156. public void OnDrag(PointerEventData eventData)
  157. {
  158. if (itemList.Count < 3) return;
  159. RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRectTran, eventData.position, eventData.pressEventCamera, out Vector2 vector);
  160. {
  161. currentPos = eventData.pointerPressRaycast.worldPosition;
  162. currentPos = vector;
  163. UpdateList(currentPos);
  164. }
  165. }
  166. private RectTransform viewRectTran;
  167. private void Awake()
  168. {
  169. viewRectTran = GetComponent<RectTransform>();
  170. }
  171. }