- Home /
Question by
manuelmangual · Apr 20, 2017 at 09:23 PM ·
c#unity 5eventsystem
OnDrag does not stop to detect screen bounds
I have a GUI window with a OnDrag event function. The thing is, I want the GUI window to stop when it touches any of the screen boundaries. Is that possible? Can someone please show me how do I go about doing something like this?
Here is the code that handles the "drag" event:
/// <summary>
/// Called by the EventSystem everytime the pointer is moved during dragging
/// </summary>
/// <param name="eventData">The current event data</param>
public void OnDrag(PointerEventData eventData)
{
GUIRectTransform.position = eventData.position - mCursorOffset;
}
/// <summary>
/// Evaluates the current state and transitions to the pressed state
/// </summary>
/// <param name="eventData">The EventData usually sent by the EventSystem</param>
public void OnPointerDown(PointerEventData eventData)
{
Vector2 dragBegin;
RectTransformUtility.ScreenPointToLocalPointInRectangle(GUIRectTransform, eventData.position, eventData.pressEventCamera, out dragBegin);
Vector2 rectPosition = GUIRectTransform.position;
mCursorOffset = eventData.position - rectPosition;
}
/// <summary>
/// Evaluate eventData and transition to appropriate state. New state will be based upon current hover state.
/// </summary>
/// <param name="eventData">The EventData usually sent by the EventSystem.</param>
public void OnPointerUp(PointerEventData eventData)
{
//TODO: Add logic here
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612120309im_/https://answers.unity.com/themes/thub/images/avi.jpg)