Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UIElements simple drag example

Discussion in 'UI Toolkit' started by cecarlsen, Apr 29, 2019.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    I am looking for a simple example of selecting and dragging a VisualElement. Does it exist? I have looked at the source code for ShaderGraph, but it's complex to dive into.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    RKar, mck94 and cecarlsen like this.
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    Awesome, thank you for the response.
     
  4. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    If anyone is looking that how to make your UI elements draggable then here is 4 minutes guide:
     
  5. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    103
    When you would look origin post you would notice UIElement is not "ui element" and the Visual Element is also the part of the UI Element not unity ui.

    That is little bit annoying. Unity searches simple names. Many people (including me) naming his new made scripts or little system ui element. Google searches makes really difficult to search and find things if you search for "how to drag ui elements" or "how to drag in uielements". Almost both will give same result.
     
    yatyricky, EZaca and RKar like this.
  6. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Is this still the correct method to make a VisualElement in UI Toolkit draggable? -- Or is there a different way these days?

    I am looking to rearrange elements of my list, for example.
    Or drag containers arranged in a grid to rearrange their order.
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    That method is still valid, there are other ways though. I used it to get started on drag and drop with UITK. My code has changed substantially from that example and is working with a drag and drop inventory system and a card game.
     
  8. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Mind elaborating a bit? -- There's not a whole lot of information about this kind of thing out there that I could find. D:
     
  9. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Yeah took me awhile and a few question here on the forums to get it right.

    The example in this thread is using local mouse position to move the texture around, I needed it to be for the whole screen so I can drag from inventory to equipment or a container so I changed it to work in "world" mouse coordinates. The example also uses a Manipulator to implement the dragging. I use a Manipulator for my card game, the Inventory system does not. The card game drags the element placing it into an overlay element, the inventory drags a separate element that is on top of everything else.