Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Draggable Panel - Best practice?

Discussion in 'UGUI & TextMesh Pro' started by launchpad, Aug 22, 2014.

  1. launchpad

    launchpad

    Joined:
    Aug 17, 2007
    Posts:
    95
    From what I have found, you can make a free draggable panel using Scroll Rect as an unrestricted movement type, but the other movement types (elastic/clamped) are only working where the draggable panel is larger that the Scroll Rect zone.

    What is the recommended way to have a panel freely draggable within a zone (where the panel is smaller than the zone)?
     
  2. HappyLDE

    HappyLDE

    Joined:
    Apr 16, 2014
    Posts:
    56
    I don't know what your needs really are but maybe put the small draggable on a bigger panel.
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    Hi, this isn't really a scroll view anymore and is more like a custom window component. I would recommend looking into the Drag and Drop example from the example projects and then using that as a starting point on how to make draggable elements that get clamped to the area of their parent.
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    Hi, this isn't really a scroll view anymore and is more like a custom window component. I would recommend looking into the Drag and Drop example from the example projects and then using that as a starting point on how to make draggable elements that get clamped to the area of their parent.
     
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    Hi, this isn't really a scroll view anymore and is more like a custom window component. I would recommend looking into the Drag and Drop example from the example projects and then using that as a starting point on how to make draggable elements that get clamped to the area of their parent.
     
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    Hi, this isn't really a scroll view anymore and is more like a custom window component. I would recommend looking into the Drag and Drop example from the example projects and then using that as a starting point on how to make draggable elements that get clamped to the area of their parent.
     
  7. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    Hi, this isn't really a scroll view anymore and is more like a custom window component. I would recommend looking into the Drag and Drop example from the example projects and then using that as a starting point on how to make draggable elements that get clamped to the area of their parent.
     
  8. launchpad

    launchpad

    Joined:
    Aug 17, 2007
    Posts:
    95
    Hi Tim, yes I reviewed that example but it was for dragging an icon. I wanted to drag the entire panel which (obviously common practice). If the panel is smaller than the drag area (scroll rect) it won't move unless in unrestriced mode?
     
  9. Metricton

    Metricton

    Joined:
    Jan 3, 2013
    Posts:
    13
    Launchpad you mean drag the panel as if it was a movable application window like on Mac or Windows?

    If so, boy do I have the solution for you which is even simpler than using the built in onDrag events.

    http://pastebin.com/Y2rJmfXM

    Simply create a Draggable.cs file and paste that into it. Attach the script to the object, assign a target to it and viola. Though, make sure the object you are attaching the script to is an image / text / ui component or has the Selectable component added to it.

    It is just a quicky I did for myself for my project. I haven't fully thought through the shouldReturn aspect of it quite yet.

    I use it like so: I have a window with a title bar / text area and set the script to that object and set the target to the main parent panel / window / whatever. By dragging on the title bar it will move the target. You can basically also just assign it to the panel and have it target itself.
     
  10. launchpad

    launchpad

    Joined:
    Aug 17, 2007
    Posts:
    95
    NICE WORK Metricon. Exactly what I was looking for. Plug 'n play worked. Now off to look at the code.
     
  11. Riolis

    Riolis

    Joined:
    Aug 23, 2014
    Posts:
    8
    Here is my version for drag-able window/panel.
    Works very well and the dragging is pretty precise.

    http://pastebin.com/Qm1i6bFN

    <3 the draggable stuff, was a pain in the backside implementing it with legacy GUI and toolkit2d

    edit : accidentally deleted m_transform declaration
     
    Last edited: Aug 23, 2014
  12. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    So with this the 'better' solution is Riolis'. This is because there is no update function used. This means that the dragging code is only called when dragging is happening, not on every update.
     
  13. Metricton

    Metricton

    Joined:
    Jan 3, 2013
    Posts:
    13
    Tim, when will the code be available to look at for the new GUI stuff? Because, I am curious how y'all are not using an Update to get the mouse position. Even your own documentation for the current OnMouseDrag, in MonoBehavior, states:

    So, according to the documentation, OnMouseDrag is called every frame while the mouse remains down on the object. How is this any different than getting the position in Update(), with a simple if statement, which is also called every frame? As I see it, the OnMouseDrag and Update are just two different functions that are called during the same engine loop. One just happens to be called if the mouse is down on the object, while the other is always called. Unless, I am missing something? If you really wanted to not call it every frame, then you would have to allow unity scripts to hook into actual OS mousemove / touchmove events, which IMO would be the proper way to do it, rather than call OnMouseDrag every frame. Is this what the new GUI system does with the OnDrag? I am guessing no, since most of the new stuff are just scripts themselves. Same way for Keyboard / Gamepad, I noticed that there really isn't an Event system to listen for KeyboardKeyDown and such based on actual OS events.
     
  14. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    So there is an Update happening, it's just that it's happening in the EventSystem. The event system then tells the Dragable object to be dragged. This means that there doesn't need to be an update loop on each draggable object. The less updates you have in your application the better :)
     
  15. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,803
    I use a single boolean in the update "startDrag". I set it to true in a function outside Update() named StartDragging() and set it to false when the pointer is up in another function StopDragging(). If true in Update then it calls the UpdateWidget() function where I do my thang. I do not think simple boolean switches, being the ONLY thing handled in an Update loop will be a performance killer.
     
  16. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    It's won't be a performance killer but unity has to issue a call from c++ to c# to call the update and if you have a few hundred / thousand of things like this it will start to add up.
     
    Stormy102 likes this.
  17. launchpad

    launchpad

    Joined:
    Aug 17, 2007
    Posts:
    95
    Thanks Riolis, I also like to keep as many things out of Update as possible. Works nicely for my needs.
     
  18. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    373
    This is off topic, but since nobody else mentioned it; am I the only one who sees that you posted the same message 5 times? o_O
     
  19. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,194
    I had some internet problems on a bad wifi connection... this was the result :(
     
  20. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    511
    If you have a window with a title bar where you only want to drag on the title bar. Then you can make a very simple adjustment to Riolis code like this. Remember to place the drag window component onto the UI gameobject you want to drag.

    using UnityEngine;

    using UnityEngine.EventSystems;

    publicclassdg_VLDragWindow : MonoBehaviour, IDragHandler

    {

    publicRectTransform m_transform =null;

    publicvoid OnDrag(PointerEventData eventData)


    {

    m_transform.position +=newVector3(eventData.delta.x, eventData.delta.y);

    // magic : add zone clamping if's here.

    }

    }
     
    Mazak likes this.
  21. Ohadqumpo

    Ohadqumpo

    Joined:
    Jul 28, 2013
    Posts:
    1
    Metricton (Post #9): Your solution is simple, understandable, and brilliant. I commend you for it!
     
  22. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    Perfectly simple solution! Thank you so much. Nicely done.