• 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
1
Question by MikezNesh · Jul 13, 2010 at 06:55 PM · drag-and-drop

On Drag Release?

Is there such a thing called OnDrag Release or something like it?

So I could do something like if onDrag Release is on a button then blah happens.

Thanks.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by skovacs1 · Jul 13, 2010 at 08:08 PM

No. There is no such function built-in per se.

This question may be related to this post depending on your use case and there are a few drag scripts in the wiki.

There are several ambiguities to this question that depend on your use case, such as: Are you dragging entirely within something? Can you drag off and back onto something? Are you dragging something onto something else? When dragging one thing onto another, is the thing being dragged in between the cursor and the thing you are dragging it over? etc.

As Daniel has said, you can use the MouseButtonUp event. If you combine this with other events and functions, you can approximate a drag release event.

If you want something simple, then you can just attach somthing like this to what you want the mouse to drag release on - Collider or GUIElement: (Does not handle objects in between, selections or dragging internal to something (which would require some sort of checks to differentiate from clicking)).

//Unity mono public class DragRelease: MonoBehaviour { bool dragging = false;

 void OnMouseEnter()
 {
     //If the left mouse button is down when we enter, we're dragging over.
     if(Input.GetMouseButton(0)) { dragging = true; }
 } //MouseEnter

 void OnMouseExit()
 {
     //If we're leaving, then we aren't dragging over this.
     dragging = false;
 } //MouseExit

 void OnMouseUp()
 {
     //If we were dragging, then we just released over this.
     if(dragging) { /*do something*/ }

     //We're not dragging anymore
     dragging = false;
 } //MouseUp

} //DragRelease

//Unity js var dragging : bool = false;

function OnMouseEnter() { //If the left mouse button is down when we enter, we're dragging over. if(Input.GetMouseButton(0)) { dragging = true; } } //MouseEnter

void OnMouseExit() { //If we're leaving, then we aren't dragging over this. dragging = false; } //MouseExit

void OnMouseUp() { //If we were dragging, then we just released over this. if(dragging) { /do something/ }

 //We're not dragging anymore
 dragging = false;

} //MouseUp

This is very basic and general. Given as you are likely going to be selecting and dragging some thing over this, I would recommend that you check against your selection to ensure that they are dragging something at all and dragging a valid something. You might even check (in the OnMouseUp for example) against a variable exposed on your selectable objects or a general input script to indicate that they are dragging something and in stead check for dragging in the scripts of the selectable objects (OnMouseDown and OnMouseUp depending on how you mean to drag).

There is a MouseDrag event which is called every frame while the mouse is held down over the object to which the script is attached and this may be of use for your selections.

Since the mouse pick is using a raycast, I'm not entirely sure how this might work if what you're dragging is actually between the mouse position and what you are dragging over, so be warned. If it's a problem in that case, you may have to check against the mouse position and the GUIElement or something either using screen coordinates or your own raycast, etc.

Comment
Add comment · Show 2 · 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 MikezNesh · Jul 13, 2010 at 08:13 PM 0
Share

Could you turn that into javascript? Thanks.

avatar image skovacs1 · Jul 14, 2010 at 02:29 PM 0
Share

Sure, just replace the 'void's with 'function's and change the 'bool dragging' to 'var dragging : bool' and remove the DragRelease class.

avatar image
2
Best Answer

Answer by Daniel 6 · Jul 13, 2010 at 07:11 PM

There is the method Input.GetMouseButtonUp(). This is how it's used:

function Update () {
   if (Input.GetMouseButtonoUp (0)) {
      // This will be executed when the mouse button was released.
   }
}

Or if you just want a button, here is how you can do it:

function OnGUI () {
   if (GUILayout.Button ("I am a button")) {
      // This is executed when the button is pressed.
   }
}
Comment
Add comment · 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
2

Answer by Rarceth · Jun 28, 2018 at 12:37 PM

old thread, but I just want to update. There is an interface you can use for dragging. If you use the correct library:

 using UnityEngine.EventSystems;

you gain access to a few different pointer interfaces. These include pointer enter, exit,etc. The important ones here are the IBeginDragHandler, IDragHandler, IEndDragHandler. For this post specifically, you can use the IEndDragHandler with an appropriate function to perform actions on the end of a drag of a ui object. E.g

 public void OnEndDrag(PointerEventData eventData)
         {
         //Destroy my dragged object
             if (meDrag != null)
                 Destroy(meDrag);
         }

Good luck to all :)

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Drag and drop 3d game 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers

Drag and droping from GUI 1 Answer

Spawn new object after last one is dragged away 0 Answers

How do I detect OnDrop event on a GameObject? 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