Unity3D UI, calculation for position dragging an item?

Fattie

These days it's incredibly easy to drag UI elements in Unity: Make a few UI items. Add Component -> Event -> Event Trigger. Drop on the script below. Click to add the four obvious triggers. You're done.

However.

I'm totally lost in the relationship between pointer coordinates and UI coordinates (as seen in RectTransform and so on).

In DragIt below: how the hell do you move a UI panel correctly under the finger?

Say you have one large panel, with ten UIButton sitting in the panel with Dragster on the buttons. What is the relationship between the RectTransform coords and the mouse pointer ...

in short how do you move one of the button around at DragIt() below?

/* modern Unity drag of UI element */
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class Dragster:MonoBehaviour
    {
    public int index; // number each of your UI items
    static bool beingDragged = false;
    static int dragFrom;
    public void DragStart()
        {
        beingDragged = true; dragFrom = index;
        }
    public void DragIt()
        {
        ? ? W T F ? ?
        }
    public void DragEnd()
        {
        beingDragged = false;
        }
    public void DroppedBra()
        {
        Debig.Log("Drag: from/to " +dragFrom +" --> " +index);
        }
    }
Uri Popov

For Draging stuff I just do this :

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
    
    
    
    public void OnBeginDrag(PointerEventData eventData) {
        
    }
    
    public void OnDrag(PointerEventData eventData) {
        //Debug.Log ("OnDrag");
        
        this.transform.position = eventData.position;

        }

    public void OnEndDrag(PointerEventData eventData) {
        Debug.Log ("OnEndDrag");
    
    }
}

Here's the identical amazing URIPOPOV CODE with two simple features which you always need when dragging:

// allow dragging with two basic problem fixes:
// - limit drag to the parent box
// - don't "jump" based on where you hold down

100% tested:

using UnityEngine;
using UnityEngine.EventSystems;

public class AmazingUPDrag : MonoBehaviour,
               IBeginDragHandler, IDragHandler, IEndDragHandler
{
    Vector2 dragOffset = Vector2.zero;
    Vector2 limits = Vector2.zero;

    public void OnBeginDrag(PointerEventData eventData)
    {
        dragOffset = eventData.position - (Vector2)transform.position;
        limits = transform.parent.GetComponent<RectTransform>().rect.max;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position - dragOffset;
        var p = transform.localPosition;
        if (p.x < -limits.x) { p.x = -limits.x; }
        if (p.x > limits.x) { p.x = limits.x; }
        if (p.y < -limits.y) { p.y = -limits.y; }
        if (p.y > limits.y) { p.y = limits.y; }
        transform.localPosition = p;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        dragOffset = Vector2.zero;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unity3D : Blur the background of a UI canvas

From Dev

jQuery UI sortable connected list item snaps to second last position

From Dev

jQueryUI Sortable Item Incorrect Position While Dragging

From Dev

How do I position JQuery UI Sortable item at mouse cursor while dragging?

From Dev

Javascript d3: Is there a way to programmatically stop dragging an item?

From Dev

Updated position Unity3d

From Dev

Unity3D new UI System and List Views

From Dev

Unity3D instantiate list of prefabs in new UI

From Dev

Unity3D SetActive freeze the UI panel

From Dev

Get touch position while dragging

From Dev

Unity3D: How to programmatically test new UI features

From Dev

In Unity3d How detect touch on UI or not?

From Dev

Unity3D - Screen Capture of particular UI elements

From Dev

Creating navigation between Unity3D instantiated UI elements

From Dev

Alternative of Raycast for UI Items in Unity3D

From Dev

Unity3D OnPointerClick is based on cam position?

From Dev

Unity3D position Update when gameObject is changed

From Dev

Dragging/Dropping, returning to original position, and then dragging again with jquery UI

From Dev

Unity3D Math calculation issues

From Dev

Javascript d3: Is there a way to programmatically stop dragging an item?

From Dev

Unity3D instantiate list of prefabs in new UI

From Dev

Is it possible to make the Unity3D UI slider that follows a timeline?

From Dev

Unity3D: How to programmatically test new UI features

From Dev

Dragging an imageview to certain position

From Dev

Unity3D UI, calculation for position dragging an item?

From Dev

Dragging Item With Icon Following Pointer Unity C#

From Dev

Unity3D OnPointerClick is based on cam position?

From Dev

Unity3D C# - Text UI

From Dev

freezing the y position of the camera in unity3d

Related Related

  1. 1

    Unity3D : Blur the background of a UI canvas

  2. 2

    jQuery UI sortable connected list item snaps to second last position

  3. 3

    jQueryUI Sortable Item Incorrect Position While Dragging

  4. 4

    How do I position JQuery UI Sortable item at mouse cursor while dragging?

  5. 5

    Javascript d3: Is there a way to programmatically stop dragging an item?

  6. 6

    Updated position Unity3d

  7. 7

    Unity3D new UI System and List Views

  8. 8

    Unity3D instantiate list of prefabs in new UI

  9. 9

    Unity3D SetActive freeze the UI panel

  10. 10

    Get touch position while dragging

  11. 11

    Unity3D: How to programmatically test new UI features

  12. 12

    In Unity3d How detect touch on UI or not?

  13. 13

    Unity3D - Screen Capture of particular UI elements

  14. 14

    Creating navigation between Unity3D instantiated UI elements

  15. 15

    Alternative of Raycast for UI Items in Unity3D

  16. 16

    Unity3D OnPointerClick is based on cam position?

  17. 17

    Unity3D position Update when gameObject is changed

  18. 18

    Dragging/Dropping, returning to original position, and then dragging again with jquery UI

  19. 19

    Unity3D Math calculation issues

  20. 20

    Javascript d3: Is there a way to programmatically stop dragging an item?

  21. 21

    Unity3D instantiate list of prefabs in new UI

  22. 22

    Is it possible to make the Unity3D UI slider that follows a timeline?

  23. 23

    Unity3D: How to programmatically test new UI features

  24. 24

    Dragging an imageview to certain position

  25. 25

    Unity3D UI, calculation for position dragging an item?

  26. 26

    Dragging Item With Icon Following Pointer Unity C#

  27. 27

    Unity3D OnPointerClick is based on cam position?

  28. 28

    Unity3D C# - Text UI

  29. 29

    freezing the y position of the camera in unity3d

HotTag

Archive