• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by MrMMatricks · Feb 03, 2015 at 11:51 AM · uidrag-and-dropdrag and drop

Cancel a drag and drop movement

Hello!

I want to cancel a drag and drop movement programmatically. Is there a way to do this?

I am using right now the interfaces IBeginDragHandler, IDragHandler, IEndDragHandler.

What I am trying to do: I want to cancel the dragging when I leave a certain area. So when I leave the OnEndDrag Method should be called and the drag is ended.

PS: Right now I have an internal variable to see if am still dragging and stop it if I leave the area. Right now, OnEndDrag is called twice. not really ideal.

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  using UnityEngine.EventSystems;
  public class DraggableCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
      public Text nameText;
      Vector3 startPosition;
      Transform startParent;
      bool dragging = false;
      public EventTrigger parentBox;
      void Start () {
          EventTrigger.Entry entry = new EventTrigger.Entry();
          entry.eventID = EventTriggerType.PointerExit;
          entry.callback = new EventTrigger.TriggerEvent();
          UnityEngine.Events.UnityAction<BaseEventData> callback =
              new UnityEngine.Events.UnityAction<BaseEventData>(PointerExit);
          entry.callback.AddListener(callback);
          parentBox.delegates.Add(entry);
      }
      public string Info {
          get { return nameText.text; }
          set { nameText.text = value; }
      }
      public void OnBeginDrag (PointerEventData eventData)
      {
          dragging = true;
          startPosition = transform.position;
          startParent = transform.parent;
      }
      public void OnDrag (PointerEventData eventData)
      {
          if (dragging) {
              Vector3 pos = transform.position;
              pos.x = eventData.position.x;
              transform.position = pos;
          }
      }
      public void OnEndDrag (PointerEventData eventData)
      {
          if (dragging) {
              StartCoroutine(SmoothMove (transform.position, startPosition,

0.2f)); dragging = false;

          }
      }
      public void PointerExit(BaseEventData data) {
          Debug.Log ("We Left!");
          OnEndDrag (null);
      }
  }

 

Thanks, Matthias

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Feb 03, 2015 at 12:10 PM 0
Share

Is it causing issues with OnEndDrag being called twice?

You appear to handle the issue by setting a bool so all that should be happening is it calls OnEndDrag again when you release the mouse, but that does nothing.

If so then I wouldn't be too worried about it. If not what is happening?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by MilesAdamson · Dec 02, 2015 at 10:01 AM

This question is quite old but I did find the answer.

Only allow on drag to work if a flag is set

Make the flag false if you exit the zone so the drop function stops working

Make an OnPointerUp function which sets the flag again, so that you can drag again

Reset the position of the object if OnDrag is called but the flag is false

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moh05 · Oct 18, 2016 at 11:38 AM 0
Share

Hi,

Using flag does stop dragging, but I need need to call OnEngDrag()

How can i do that?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NGUI Mouse position doesn't match with dragged object 0 Answers

Maximize On Play UI is small???? 2 Answers

OnDrop called on wrong GameObject 1 Answer

Determining if Horizontal Layout Group has room for an element. 0 Answers

Left end of ui slider background flattens when moving handle to left 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges