如今,在 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/