How to drag a UI object?

Alrighty i know this was asked once before ive seen some of the answers and it seems like unity has changed the ways of doing it.

I just want a little guidance. i’m not sure if this is supposed to be done through scripting or if its already built in for us. Any help would be great.

The important thing is to make sure you ‘Implement’ the interface. This must be done for EACH interface you use. (Also I believe the functions must be public).

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Test : MonoBehaviour, IDragHandler{
	public void OnDrag(PointerEventData eventData)

The list of interfaces is on this page.

For example,

public void OnPointerEnter(PointerEventData eventData){}

must implement ‘IPointerEnterHandler’.

In uJS, you need the ‘implements’ keyword

public class Test extends MonoBehaviour implements IDragHandler

If you’re talking dragging in-game at runtime, you do it by scripting. In the editor it just works as moving any other object.

You said you’ve seen answer and “it seems like unity has changed the ways of doing it”. What do you mean? You tried that solution in 4.6.x, it worked, and it doesn’t work in Unity 5.x? What’s that answer you’re talking about?

I’ve done this in Unity 4.6.x and Unity 5.x the same way, detecting when the pointer enters or leaves the UI element and setting a bool variable to true or false, then on the Update if the mouse button is being held down and that bool is true, I move the UI element to the mouse position.

I guess there are other approaches to this, there are the OnPointerDragXXX events that I guess should be used for this.