docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Click on a visual element

    Use the Click() method to simulate a mouse click on a visual element in your tests. This method sends the appropriate mouse events to the target element, mimicking a real user interaction.

    The following examples show how to simulate a click on a button:

    [Test]
    public void ClickExample()
    {
        // Make sure the UI is totally up to date.
        simulate.FrameUpdate();
    
        var actionWasExecuted = false;
    
        // Set up the button's clicked functionality to flip a Boolean.
        Button button = rootVisualElement.Q<Button>("MyButton");
        button.clicked += () => { actionWasExecuted = true; };
    
        // Click on the button's position.
        simulate.Click(button);
    
        // Ensure the UI Toolkit update loop executes.
        // This step isn't required for this specific case, 
        // because this button just flips a Boolean when it's clicked.
        // However, if the button performs actions linked to the UI Toolkit 
        // update loop, like styling or scheduling, you need to run the 
        // update loop after clicking.
        simulate.FrameUpdate();
    
        Assert.That(actionWasExecuted, Is.True,
            "Button action was not executed.");
    }
    

    Additional resources

    • Double-click on 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)