VisualElement Drag and Drop during runtime.

could you provide a link to that…?

Certainly.

1 Like

I know that this is an older thread - but I thought I’d post a link to my tutorial incase anyone else comes across this. It covers how I tackled drag/drop for an inventory in my game.

https://gamedev-resources.com/create-an-in-game-inventory-ui-with-ui-toolkit/

3 Likes

I made a little runtime drag-and-drop system for UIToolkit. MIT licensed.

In doing that I wrote a ChangeParent() method that does what you asked. It was conceptually simple but tricky to get right because of when changes take effect. Basically it required a well placed schedule.Execute().

  public static IVisualElementScheduledItem ChangeParent(VisualElement target,
                                                         VisualElement newParent) {
    var position_parent = target.ChangeCoordinatesTo(newParent, Vector2.zero);
    target.RemoveFromHierarchy();
    target.transform.position = Vector3.zero;
    newParent.Add(target);
    // ChangeCoordinatesTo will not be correct unless you wait a tick. #hardwon
    return target.schedule.Execute(() => {
      var newPosition = position_parent - target.ChangeCoordinatesTo(newParent,
                                                                     Vector2.zero);
      target.RemoveFromHierarchy();
      target.transform.position = newPosition;

      newParent.Add(target);
    });
  }
5 Likes

I love the design of your game!

1 Like