Unity Edit Drag and Drop GUILayout.Box

Hello guys,

My name is Víctor and I’m trying to make my own visual Behaviour Tree Editor in order to make IA faster.

I’m starting with Editor Customization in Unity so by now my doubt is about making drag and drop of a box ( will be a node on my tree ) to re-position and later to connect with another box.

My code is:

private void OnGUI(){

        gridRect = new Rect(verticalRect.width, 0, position.width-verticalRect.width, position.height);
        GUILayout.BeginArea(gridRect);
        GUI.color = Color.white;
        GUI.Box(new Rect(500, 500, 100, 100), "test");

        DropAreaGUI();

        GUILayout.EndArea();
}

and

   private Vector2 currentMousePosition;
    private bool isDragging = false;
    Vector2 mouseDragStart;

    public void DropAreaGUI()
    {
        Event evt = Event.current;
        //GUI.Box(gridRect, "Add Trigger");

        switch (evt.type)
        {
            case EventType.DragUpdated:
                break;
            case EventType.MouseDown:
                mouseDragStart = evt.mousePosition;
                Debug.Log(mouseDragStart);
                break;
            case EventType.MouseDrag:
                if (evt.type == EventType.MouseDrag)
                {
                    currentMousePosition = Event.current.mousePosition;

                }
                Debug.Log(currentMousePosition);
                GUI.Box(new Rect(currentMousePosition.x,currentMousePosition.y,100,100), "test");
                Repaint();
                break;
              
        }
        if (evt.isMouse)
        {
            evt.Use();
        }
    }

Here you find and Image of the box I would like to move “Test” box.

Any idea ?
My approach is to “re-paint” the box while updating mousePosition but it’s not working.
I tried with many tutorials on forum questions but doesn’t work, they seem to work about GameObjects or items in inspector.

Regards, Víctor.

Okey guys, I found in StackOverflow a really simple solution since in Unity Forums the solutions I found was quite more complex.

Rect _boxPos =  new Rect(0, 0, 100, 100);

void OnGUI()
{
    if (Event.current.type == EventType.MouseDrag &&
        _boxPos.Contains(Event.current.mousePosition))
    {
        _boxPos.position += Event.current.delta;
    }

    GUI.Box(_boxPos, "test");

    if (Event.current.isMouse)
        Event.current.Use();
}

You just have to declare Rect Box Position as a global variable so if the MouseDrag position is contained in _boxPos we add to _boxPos the mouse Movement.

Event.current.Use() is used to repaint.

Solution in stackoverflow is made by Alaanor, thanks to him.