Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Get all Buttons in a VisualElement.

Discussion in 'UI Toolkit' started by DressedToParty, Nov 19, 2020.

  1. DressedToParty

    DressedToParty

    Joined:
    Mar 7, 2019
    Posts:
    4
    Hello, I am just curious if there is a way to get all the Buttons in a Visual Element?
    You can grab single ones with the query and its name. I was just wondering if there is a way to get all Buttons, Text, or lables etc returned in an Array or something. I could not find anything in the documentation.

    Thank you.
     
  2. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    811
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    andrew-lukasik and Autarkis like this.
  4. DressedToParty

    DressedToParty

    Joined:
    Mar 7, 2019
    Posts:
    4
    Thank you MousePods, and florianhanke. That is exactly what I was looking for!
     
    florianhanke and MousePods like this.
  5. tacman1123

    tacman1123

    Joined:
    Sep 14, 2020
    Posts:
    77
    Is there a way to get everything? Or everything of a particular class?

    root = GetComponent<UIDocument>().rootVisualElement;

    like root.Query<>() or root.Query<*>(), but neither of those are right.

    root.Query<VisualElement>() doesn't give an error, but doesn't return anything.

    I tried visualElement.Children(), but that's not it either.

    I'd also like to filter by class, e.g.

    root.Query(null, ['my-class']).ForEach( e => Debug.Log(e.ToString()) );

    but that's not working either.

    Thanks.
     
    NMJ_GD, Zixua and GeneralTsao like this.