Hi I created a UI canvas with some UI child elements on it. The dimensions of the canvas are 1600 x 800, with 0 in the centre. I have been trying to learn how to use the IDragHandler interface to drag some UI elements around on the canvas. However, I have a problem that I don't understand. The x y recttransform values of the UI Image to be dragged might start out at -500, -200, for example, but as soon as I try to drag the element, its x y values in the rect transfrom sky rocket to five figures and it disappears off canvas somewhere. All I'm doing on the dragged object is this: Code (CSharp): public class ItemDragHandler : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { transform.position = Input.mousePosition; } } What might I be doing wrong? Cheers
You need to also implement IBeginDragHandler and IEndDragHandler. Unity ignores your drag handler unless you also implement those. They don't have to actually DO anything, but you need to have them. (Your confusion is perfectly reasonable; this is a stupid and misleading way for Unity to have organized things, and they don't even warn you in the documentation. It's just a magical thing you have to know.)
Thanks. I've added the interfaces but it behaves exactly the same. Also, what do you put in onBegin and OnEnd? I don't need anything special to happen here... Code (CSharp): public class ItemDragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public void OnBeginDrag(PointerEventData eventData) { //transform.position = ; } public void OnDrag(PointerEventData eventData) { transform.position = Input.mousePosition; } public void OnEndDrag(PointerEventData eventData) { //transform.position = ; } }
It looks to me like that code should do something, so I'm not sure what might be stopping it. I'd try adding a Debug.Log statement to check whether OnDrag is running at all. However, note that the coordinate system for the mousePosition is in pixels rather than Unity game units, so that code won't necessarily make the game object follow the mouse in the way you are probably expecting. This page includes an example of how to make an object follow the cursor while you are dragging. Alternately, here's some code I wrote (based on the above) for dragging a UI object in my game: Code (CSharp): private bool dragging = false; private Vector3 dragOffset; public void OnBeginDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return; dragging = true; Vector3 worldpoint; if (DragWorldPoint(eventData, out worldpoint)) { dragOffset = GetComponent<RectTransform>().position - worldpoint; } else { dragOffset = Vector3.zero; } } public void OnEndDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return; dragging = false; } public void OnDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return; Vector3 worldpoint; if (DragWorldPoint(eventData, out worldpoint)) { RectTransform rt = GetComponent<RectTransform>(); rt.position = worldpoint + dragOffset; } } // Gets the point in worldspace corresponding to where the mouse is private bool DragWorldPoint(PointerEventData eventData, out Vector3 worldPoint) { return RectTransformUtility.ScreenPointToWorldPointInRectangle( GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out worldPoint); }