现在,在Unity中拖动UI元素非常容易:制作一些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); } }对于Draging的东西我只是这样做:
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"); } }