docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Drag an element from one place to another

    To drag an element from the specified positionFrom to the specified positionTo, use the DragAndDrop() method from the PanelSimulator class.

    The following example shows how to use the DragAndDrop function to drag on a slider:

    [Test]
    public void MouseDragExample()
    {
        // Make sure the UI is totally up to date.
        simulate.FrameUpdate();
    
        // Fetch and focus the Slider.
        var slider = rootVisualElement.Q<Slider>("MySlider");
        var dragger = slider.Q<VisualElement>("unity-dragger");
        slider.Focus();
        simulate.FrameUpdate();
    
        Assume.That(dragger.ClassListContains("unity-base-slider--movable"));
        Assume.That(slider.hasFocusPseudoState, Is.True);
    
        var startingPosition = slider.value;
    
        // Send RightArrow key presses.
        simulate.DragAndDrop(dragger.worldBound.center,
            dragger.worldBound.center + new Vector2(10, 10));
        simulate.FrameUpdate();
    
        var endingPosition = slider.value;
        Assert.That(endingPosition, Is.GreaterThan(startingPosition),
            "Slider value did not update as expected.");
    }
    

    Additional resources

    • Move the mouse to a visual element
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)