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.

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.
   }
}

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 :slight_smile:

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.