using System; using System.Collections.Generic; using SC.XR.Unity.Module_InputSystem; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace GHZLangChao { public class CustomVerticleScrollView : MonoBehaviour, IPointerDownHandler { public List data = new List(); public RectTransform content; public List itemList = new List(); private Vector3 lastPosition; private float offset; public int startIndex = 0; public int endIndex = 0; private float size; private float index0Y; private bool isMoveItem; // 标记当前的Item是否需要挪动 private bool sliderUp; private bool isDrag; public float moveSpeed = 2000; private void Awake() { for (int i = 0; i < 20; i++) // 添加10个数据 { data.Add("4A-"+i.ToString()+"机房"); } //data = MachineRoomManager.Instance.listRoomName; size = itemList[0].GetComponent().rect.size.y; index0Y = itemList[0].transform.localPosition.y; for (int i = 0; i < itemList.Count; i++) // 显示几个数据(这里为5个) { itemList[i].GetComponentInChildren().text = data[i]; } startIndex = 0; endIndex = itemList.Count - 2; DispatcherBase.KeyDownDelegateRegister(downEvent); } private void downEvent(InputKeyCode keyCode, InputDevicePartBase part) { if (part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null) { checkMoveDown(part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject, part); } } public void checkMoveDown(GameObject obj, InputDevicePartBase part) { if (obj) { if (obj.name==this.gameObject.name) { Debug.Log("OnPointerDown"); isDrag = true; lastPosition = part.inputDataBase.SCPointEventData.position; } else { if (obj.transform.parent != null) checkMoveDown(obj.transform.parent.gameObject, part); } } } private void UpdateList(Vector2 position) { offset = position.y - lastPosition.y; offset = Mathf.Clamp(offset, -75, 75); // Item高/2 lastPosition = position; for (int i = 0; i < itemList.Count; i++) { itemList[i].transform.localPosition += Vector3.up * offset; } if (offset > 0) // 往上滑动 { if (!sliderUp && isMoveItem) { startIndex++; if (startIndex > data.Count - 1) startIndex = 0; isMoveItem = false; } sliderUp = true; if (itemList[0].transform.localPosition.y >= index0Y && !isMoveItem) { isMoveItem = true; Debug.Log("添加一个Item到末尾"); itemList[itemList.Count - 1].transform.localPosition = itemList[itemList.Count - 2].transform.localPosition - Vector3.up * size; endIndex++; if (endIndex > data.Count - 1) endIndex = 0; itemList[itemList.Count - 1].GetComponentInChildren().text = data[endIndex]; } else { if (itemList[0].transform.localPosition.y >= index0Y + size) { isMoveItem = false; Debug.Log("将第一个Item放到末尾"); RoomName_Item temp = itemList[0]; temp.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition - Vector3.up * size; for (int i = 1; i < itemList.Count; i++) { itemList[i - 1] = itemList[i]; } itemList[itemList.Count - 1] = temp; startIndex++; if (startIndex > data.Count - 1) startIndex = 0; } } } else if (offset < 0)// 往下滑动 { if (sliderUp && isMoveItem) { endIndex--; if (endIndex < 0) endIndex = data.Count - 1; isMoveItem = false; } sliderUp = false; if (itemList[0].transform.localPosition.y <= index0Y && !isMoveItem) { isMoveItem = true; Debug.Log("添加一个Item到顶部"); itemList[itemList.Count - 1].transform.localPosition = itemList[0].transform.localPosition + Vector3.up * size; startIndex--; if (startIndex < 0) startIndex = data.Count - 1; itemList[itemList.Count - 1].GetComponentInChildren().text = data[startIndex]; } else { if (itemList[0].transform.localPosition.y <= index0Y - size) { Debug.Log("将最后一个Item放到顶部"); RoomName_Item temp = itemList[itemList.Count - 1]; temp.transform.localPosition = itemList[0].transform.localPosition + Vector3.up * size; isMoveItem = false; for (int i = itemList.Count - 1; i >= 1; i--) { itemList[i] = itemList[i - 1]; } itemList[0] = temp; endIndex--; if (endIndex < 0) endIndex = data.Count - 1; } } } } private Vector3 targetPos; private Vector3 currentPos; private void Update() { if (isDrag) { if (Input.GetMouseButtonUp(0)) { isDrag = false; targetPos = Input.mousePosition + Vector3.Project(Input.mousePosition - currentPos, Vector3.up) * moveSpeed; } else { currentPos = Input.mousePosition; UpdateList(currentPos); } } else { currentPos = Vector3.Lerp(currentPos, targetPos, Time.deltaTime); UpdateList(currentPos); } } public void OnPointerDown(PointerEventData eventData) { isDrag = true; lastPosition = eventData.position; } } }