Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 20, 2015 at 07:48 PM by Xeong-Hu for the following reason:

Other

avatar image
2
Question by Xeong-Hu · Oct 18, 2015 at 10:43 AM · uidrag

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
2

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
 {
 }


Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiegoSLTS · Oct 19, 2015 at 03:35 AM 0
Share

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.

avatar image meat5000 ♦ DiegoSLTS · Oct 19, 2015 at 08:45 AM 0
Share

If you want OnDrag to appear in your script, not implementing it throws an error.

avatar image DiegoSLTS meat5000 ♦ · Oct 19, 2015 at 01:55 PM 0
Share

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".

Show more comments
avatar image Xeong-Hu DiegoSLTS · Oct 19, 2015 at 08:52 AM 1
Share

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.

Image

example.png (239.3 kB)
avatar image DiegoSLTS Xeong-Hu · Oct 19, 2015 at 01:59 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xeong-Hu · Oct 18, 2015 at 02:12 PM 0
Share

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.

avatar image DiegoSLTS Xeong-Hu · Oct 18, 2015 at 02:24 PM 0
Share

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).

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ScreenPointToLocalPointInRectangle not giving output 1 Answer

How to move UI image between panels 0 Answers

Is it good idea to implement camera drag with UI drag event? 2 Answers

How to tell if a UI slider is being clicked and draged. 1 Answer

Prevent Drag button inside scroll rect 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges