public override void OnBeginDrag(PointerEventData eventData) { base.OnBeginDrag(eventData); this.m_currentMouseState = ScrollRectMouseStatus.BeginDrag; SendMouseStateChangedNotification(eventData); //获取Drag的item for (int i = 0; i < content.childCount; ++i) { Transform childObject = content.GetChild(i); RectTransform childRectTransform = childObject as RectTransform; if (RectTransformUtility.RectangleContainsScreenPoint(childRectTransform, eventData.position, eventData.enterEventCamera)) { this.m_onDraggingItemIndex = itemTypeStart + i; this.m_onDraggingGameObject = childObject.gameObject; break; } } if (OnDragItem != null && this.m_onDraggingGameObject != null) { OnDragItem(this.m_onDraggingItemIndex, this.m_onDraggingGameObject); } }
public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); this.m_currentMouseState = ScrollRectMouseStatus.EndDrag; SendMouseStateChangedNotification(eventData); this.m_onDraggingItemIndex = -1; this.m_onDraggingGameObject = null; }
public override void OnDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) { return; } if (!IsActive()) { return; } //使用PointerEventData 同时要使用eventData.neterEventCamera 否则这个接口会有问题 if (!RectTransformUtility.RectangleContainsScreenPoint(viewRect, eventData.position, eventData.enterEventCamera)) { if (m_currentMouseState == ScrollRectMouseStatus.ExitScrollRect) { if (OnMouseExitScrollRectAndDragging != null) { OnMouseExitScrollRectAndDragging(eventData); } } else if (m_currentMouseState == ScrollRectMouseStatus.Draging) { this.m_currentMouseState = ScrollRectMouseStatus.ExitScrollRect; SendMouseStateChangedNotification(eventData); } return; } else { if (m_currentMouseState == ScrollRectMouseStatus.ExitScrollRect) { m_currentMouseState = ScrollRectMouseStatus.ReEnterScrollRect; SendMouseStateChangedNotification(eventData); } } Vector2 localCursor; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(viewRect, eventData.position, eventData.pressEventCamera, out localCursor)) { return; } this.m_currentMouseState = ScrollRectMouseStatus.Draging; UpdateBoundsAndItems(); var pointerDelta = localCursor - m_PointerStartLocalCursor; Vector2 position = m_ContentStartPosition + pointerDelta; Vector2 offset = CalculateOffset(position - content.anchoredPosition); position += offset; if (movementType == MovementType.Elastic) { if (offset.x != 0) { position.x = position.x - RubberDelta(offset.x, m_ViewBounds.size.x) * rubberScale; } if (offset.y != 0) { position.y = position.y - RubberDelta(offset.y, m_ViewBounds.size.y) * rubberScale; } } SetContentAnchoredPosition(position); if (OnScrollRectDragging != null) { OnScrollRectDragging(eventData); } }