- Home /
Other
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.
Answer by meat5000 · Oct 18, 2015 at 02:36 PM
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
{
}
You don't need to implement IDragHandler if you use the EventTrigger component, just setting the events in the inspector should work, that component already handles the OnDrag event and the rest, and calls any function attached for each event.
It's a valid solution, but it's not a "must" and shouldn't be mixed with the EventTrigger component approach.
If you want OnDrag to appear in your script, not implementing it throws an error.
If you implement IDragHandler you have to implement the OnDrag(PointerEventData) method, I know that, but if you use the EventTrigger component you DON'T need to implement any interface. I just wanted to clarify that implementing the interface is ONE way of doing what the OP asked, cos you said "the important thing is" and that might be confusing since it sounds like "if you don't do this you can't detect drag events".
Thank you both for the helping hand, and srry for the late reply.
Well I tried this out but i'm still a little confused.
Here's what I did.
I made a script called Window with these codes within it.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class Window : $$anonymous$$onoBehaviour {
EventSystem FindEvent;
Event Find2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnDrag(PointerEventData EventData){
if (EventData.dragging) {
print ("Dragging");
};
//EventData.currentSelectedGameObject.transform.position = Input.mousePosition;
}
}
I then gave my Window an EventTrigger Event
Then I clicked where it says no function
After that I select my Window Script and try to find the drag function in my script but it doesn't show up.
Change the type of the function parameter to BaseEventData ins$$anonymous$$d of PointerEventData: http://docs.unity3d.com/ScriptReference/EventSystems.BaseEventData.html
Or remove it, for a function to show in that list it has to be a public function with zero or one parameter of basic types (int, float, bool, string) or the type specified in event. Check where it says "Drag (BaseEventData)" after you added the Drag event.
Answer by DiegoSLTS · Oct 18, 2015 at 12:54 PM
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.
Thanks for giving me a hand.
Well I never really tried it back in the odler version of unity. I'm totally new to this. So I tried doing this solution in an answer here.
http://answers.unity3d.com/questions/846360/c-draggable-46-ui.html
I read Crixu's comment and noticed he was right so then it made me feel stoped.
I've been trying to figure this out but I can't get a solution. I tried getting the mouseposition from the Eventsystem but it's not possible.
Well, I'm not sure why he said that, OnDrag is still there according to the docs: http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
Try the solution posted there, if it doesn't work come back and explain where you got stuck.
EDIT: In the inspector of the EventTrigger component you add the "Drag" event. All the "OnXXX" functions of the EventTrigger are displayed without the "On" in the inspector (OnPointerDown is displayed as PointerDown, etc, etc).