CustomHorizintalScrollView.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Debug.Log("UpdateSceneItem411");
  23. mSceneData = m_SceneData;
  24. Debug.Log("UpdateSceneItem41");
  25. size = itemList[0].GetComponent<RectTransform>().rect.size.x;
  26. index0Y = itemList[0].transform.localPosition.x;
  27. Debug.Log("UpdateSceneItem42");
  28. if (m_SceneData.Count == 1)
  29. {
  30. itemList[2].gameObject.SetActive(false);
  31. itemList.Remove(itemList[2]);
  32. itemList[1].gameObject.SetActive(false);
  33. itemList.Remove(itemList[1]);
  34. Vector3 temp = itemList[0].gameObject.GetComponent<RectTransform>().localPosition;
  35. temp.x = 600;
  36. itemList[0].gameObject.GetComponent<RectTransform>().localPosition = temp;
  37. }
  38. else if (m_SceneData.Count == 2&& itemList.Count==3)
  39. {
  40. itemList[2].gameObject.SetActive(false);
  41. itemList.Remove(itemList[2]);
  42. }
  43. Debug.Log("UpdateSceneItem43");
  44. for (int i = 0; i < itemList.Count; i++) // 显示几个数据(这里为5个)
  45. {
  46. itemList[i].GetComponentInChildren<Text>().text = mSceneData[i].name;
  47. itemList[i].SceneData = mSceneData[i];
  48. }
  49. Debug.Log("UpdateSceneItem44");
  50. startIndex = 0;
  51. endIndex = itemList.Count - 2;
  52. Debug.Log("UpdateSceneItem45");
  53. }
  54. private void UpdateList(Vector2 position)
  55. {
  56. offset = position.x - lastPosition.x;
  57. offset = Mathf.Clamp(offset, -50, 50); // Item宽/2
  58. lastPosition = position;
  59. for (int i = 0; i < itemList.Count; i++)
  60. {
  61. itemList[i].transform.localPosition += Vector3.right * offset;
  62. }
  63. if (offset < 0) // 往左滑动
  64. {
  65. if (!sliderUp && isMoveItem)
  66. {
  67. startIndex++;
  68. if (startIndex > mSceneData.Count - 1)
  69. startIndex = 0;
  70. isMoveItem = false;
  71. }
  72. sliderUp = true;
  73. if (itemList[0].transform.localPosition.x <= index0Y && !isMoveItem)
  74. {
  75. isMoveItem = true;
  76. Debug.Log("添加一个Item到末尾");
  77. itemList[itemList.Count - 1].transform.localPosition = itemList[itemList.Count - 2].transform.localPosition + Vector3.right * size;
  78. endIndex++;
  79. if (endIndex > mSceneData.Count - 1)
  80. endIndex = 0;
  81. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[endIndex].name;
  82. itemList[itemList.Count - 1].SceneData = mSceneData[endIndex];
  83. }
  84. else
  85. {
  86. if (itemList[0].transform.localPosition.x <= index0Y - size)
  87. {
  88. isMoveItem = false;
  89. Debug.Log("将第一个Item放到末尾");
  90. SChooseItem temp = itemList[0];
  91. temp.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition + Vector3.right * size;
  92. for (int i = 1; i < itemList.Count; i++)
  93. {
  94. itemList[i - 1] = itemList[i];
  95. }
  96. itemList[itemList.Count - 1] = temp;
  97. startIndex++;
  98. if (startIndex > mSceneData.Count - 1)
  99. startIndex = 0;
  100. }
  101. }
  102. }
  103. else if (offset > 0)// 往右滑动
  104. {
  105. if (sliderUp && isMoveItem)
  106. {
  107. endIndex--;
  108. if (endIndex < 0)
  109. endIndex = mSceneData.Count - 1;
  110. isMoveItem = false;
  111. }
  112. sliderUp = false;
  113. if (itemList[0].transform.localPosition.x >= index0Y && !isMoveItem)
  114. {
  115. isMoveItem = true;
  116. Debug.Log("添加一个Item到顶部");
  117. itemList[itemList.Count - 1].transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
  118. startIndex--;
  119. if (startIndex < 0)
  120. startIndex = mSceneData.Count - 1;
  121. itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[startIndex].name;
  122. itemList[itemList.Count - 1].SceneData = mSceneData[startIndex];
  123. }
  124. else
  125. {
  126. if (itemList[0].transform.localPosition.x >= size - index0Y)
  127. {
  128. Debug.Log("将最后一个Item放到顶部");
  129. SChooseItem temp = itemList[itemList.Count - 1];
  130. temp.transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
  131. isMoveItem = false;
  132. for (int i = itemList.Count - 1; i >= 1; i--)
  133. {
  134. itemList[i] = itemList[i - 1];
  135. }
  136. itemList[0] = temp;
  137. endIndex--;
  138. if (endIndex < 0)
  139. endIndex = mSceneData.Count - 1;
  140. }
  141. }
  142. }
  143. }
  144. private Vector3 targetPos;
  145. private Vector3 currentPos;
  146. public void OnBeginDrag(PointerEventData eventData)
  147. {
  148. if (itemList.Count < 3) return;
  149. if ((eventData.button == PointerEventData.InputButton.Left))
  150. {
  151. isDrag = true;
  152. lastPosition = eventData.position;
  153. }
  154. }
  155. public void OnEndDrag(PointerEventData eventData)
  156. {
  157. if (itemList.Count < 3) return;
  158. isDrag = false;
  159. targetPos = eventData.pointerPressRaycast.worldPosition + Vector3.Project(eventData.pointerPressRaycast.worldPosition - currentPos, Vector3.right) * speed;
  160. //targetPos = Input.mousePosition + Vector3.Project(Input.mousePosition - currentPos, Vector3.right) * speed;
  161. }
  162. public void OnDrag(PointerEventData eventData)
  163. {
  164. if (itemList.Count < 3) return;
  165. RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRectTran, eventData.position, eventData.pressEventCamera, out Vector2 vector);
  166. {
  167. currentPos = eventData.pointerPressRaycast.worldPosition;
  168. currentPos = vector;
  169. UpdateList(currentPos);
  170. }
  171. }
  172. private RectTransform viewRectTran;
  173. private void Awake()
  174. {
  175. viewRectTran = GetComponent<RectTransform>();
  176. }
  177. }