Accessing multiple elements in UIDocument by class or type

The Q() method appears to return only on element, by name (or by class, but only one element).

I’m thinking of jQuery, where you can add a handler to all elements of a class with something like $(‘.danger’).onClick( … ).

So this works:

Button startButton = root.Q<Button>("start-button");
        startButton.clickable = new Clickable(() =>
        {
            SceneManager.LoadScene("AppScene");
        });

// var sceneButtons  = root.Q<Button>(null, ["scene-button"]); // returns just the first one

For example, how would I look for all buttons with a “scene-class” and assign this callback? I could then map the button name to the scene I wanted to load, or (ideally) get some data from the button itself. Maybe with the binding path or view data key?

I guess I could go through all the children of UIDocument, but that doesn’t seem right. Is there a way to get a list of all the elements matching a class under the root?

Thanks,

Tac

I think this post is what you are looking for: Get all Buttons in a VisualElement.

1 Like

Yep, perfect, thanks. So there’s a UQuery method that is inspired by jQuery, exactly what I was looking for! And the Query() method will likely be the one I actually use.

1 Like