c# - Unity3D UI,计算拖动项目的位置?

标签 c# unity3d unity3d-ui

如今,在 Unity 中拖动 UI 元素非常容易:制作一些 UI 项目。添加组件 -> 事件 -> 事件触发器。放在下面的脚本上。单击以添加四个明显的触发器。大功告成。

但是。

我完全迷失在指针坐标UI坐标之间的关系(如RectTransform等中所见)。

在下面的 DragIt 中:如何在手指下正确移动 UI 面板?

假设您有一个大面板,面板中有十个 UIButton,按钮上有 Dragster。 RectTransform坐标和鼠标指针有什么关系...

简而言之,如何在下面的 DragIt() 中移动其中一个按钮?

/* 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);
        }
    }

最佳答案

我会让你的脚本实现拖动接口(interface)

公共(public)类 Dragster:MonoBehaviour,IBeginDragHandler, IEndDragHandler, IDragHandler

这将使您的 DragIt 函数变为

public void OnDrag(PointerEventData eventData)
{
    transform.position += (Vector3)eventData.delta;
}

让您可以访问该事件的增量(鼠标移动了多少)以便能够移动您的对象。

如果您仍然宁愿使用 EventTrigger 组件(不太喜欢的方式),您只需将 DragIt 函数更改为 DragIt(PointerEventData eventData) 并使用 Dynamic下拉列表中的 EvenData 选项用于触发器接收 PointerEventData 以访问增量信息


实际上,这是一个基于 Uri 和 Colton 代码的拖放“UnityEngine.UI”项的完整解决方案。只需复制和粘贴即可。

Unity UI 的惊人复制和粘贴,无需思考,完美拖放,wtt Colton 和 Uri:

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

public class UNCDraggable:MonoBehaviour,
IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
    {
    public Image ghost;
    // note DON'T try to drag the actual item: it's not worth the hassle.
    // a problem arises where you can't have it on top (as you would want
    // visually), and still easily get the drops. always use a ghost.
    // even if you want the "original invisible" while dragging,
    // simply hide it and use a ghost. everything is tremendously
    // easier if you do not move the originals.
    
    void Awake()
        {
        ghost.raycastTarget = false;
        // (just in case you forgot to do that in the Editor)
        ghost.enabled = false;
        }
    
    public void OnBeginDrag(PointerEventData eventData)
        {
        ghost.transform.position = transform.position;
        ghost.enabled = true;
        }

    public void OnDrag(PointerEventData eventData)
        {
        ghost.transform.position += (Vector3)eventData.delta;
        }

    public void OnEndDrag(PointerEventData eventData)
        {
        ghost.enabled = false;
        }
    
    public void OnDrop(PointerEventData data)
        {
        GameObject fromItem = data.pointerDrag;
        if (data.pointerDrag == null) return; // (will never happen)
        
        UNCDraggable d = fromItem.GetComponent<UNCDraggable>();
        if (d == null)
          {
          // means something unrelated to our system was dragged from.
          // for example, just an unrelated scrolling area, etc.
          // simply completely ignore these.
          return;
          // note, if very unusually you have more than one "system"
          // of UNCDraggable items on the same screen, be careful to
          // distinguish them! Example solution, check parents are same.
          }
        
        Debug.Log ("dropped  " + fromItem.name +" onto " +gameObject.name);
        
        // your code would look probably like this:
        YourThings fromThing = fromItem.GetComponent<YourButtons>().info;
        YourThings untoThing = gameObject.GetComponent<YourButtons>().info;
        
        yourBossyObject.dragHappenedFromTo(fromThing, untoThing);
        }
    }

关于c# - Unity3D UI,计算拖动项目的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473802/

相关文章:

c# - Entity Framework - 合并两行数据

c# - 如何使用系统菜单创建无边框表单?

c# - Protobuf-net 编译为 DLL 字符串 [] 导致损坏的 dll

ios - 适用于 Unity 的 Firebase SDK 无法在 iOS 中编译

unity3d - 2D Sprite 的mipmap的目的?

c# - 如何从 IEnumerable 中选择项目?

c# - 该对象为 null,但是检查它是否为 null 返回 false

c# - 调用 LoadScene/LoadLevel 后场景中的对象变暗

unity3d - 根据 Horizo​​ntalLayoutGroup 等,更正 Unity3D 中的 'FlowLayoutGroup'