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:
/// Called by the EventSystem everytime the pointer is moved during dragging
/// <param name="eventData">The current event data</param>
public void OnDrag(PointerEventData eventData)
GUIRectTransform.position = eventData.position - mCursorOffset;
/// Evaluates the current state and transitions to the pressed state
/// <param name="eventData">The EventData usually sent by the EventSystem</param>
public void OnPointerDown(PointerEventData eventData)
RectTransformUtility.ScreenPointToLocalPointInRectangle(GUIRectTransform, eventData.position, eventData.pressEventCamera, out dragBegin);
Vector2 rectPosition = GUIRectTransform.position;
mCursorOffset = eventData.position - rectPosition;
/// Evaluate eventData and transition to appropriate state. New state will be based upon current hover state.
/// <param name="eventData">The EventData usually sent by the EventSystem.</param>
public void OnPointerUp(PointerEventData eventData)
//TODO: Add logic here
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
6 People are following this question.
Multiple Cars not working
Distribute terrain in zones
Changing between cameras
How do i have multiple objects to rely on a objects position?
Run coroutine only when player stays inside collider?