Search Unity

Getting List<VisualElement> of all VisualElements with a selector

Discussion in 'UI Toolkit' started by russelljahn, Apr 30, 2020.

  1. russelljahn

    russelljahn

    Joined:
    Jul 2, 2012
    Posts:
    3
    I've been using the evaluation runtime preview of UI Toolkit in a personal WebGL project and it's worked great! Right now in C# script, I want to:
    1. Take all VisualElements with a selector (say ".underline-on-hover").
    2. For each of those VisualElements, register a PointerEnterEvent callback.
    3. Do something cool in the callback.
    Is there an API call somewhere to get a List of all VisualElements with a selector? It'd be something analagous to GameObject.GetComponentsInChildren<T>().
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    You can use UQuery:
    Code (CSharp):
    1. rootElement.Query(className:".underline-on-hover")
    2.                      .ForEach( (element) =>
    3.                       {
    4.                            element.RegisterCallback<PointerEnterEvent>(
    5.                                  (evt) => DoSomethingCool());
    6.                       });
     
    oranchad likes this.
  3. russelljahn

    russelljahn

    Joined:
    Jul 2, 2012
    Posts:
    3
    Perfect, I didn't originally understand that parameter on Query(). Thanks!
     
  4. cpaganucci

    cpaganucci

    Joined:
    Mar 4, 2014
    Posts:
    2
    Resurrecting this thread to help anyone who comes across it and is having trouble. For the class name do not include the "." - so in this case it should be:

    Code (CSharp):
    1. className: "underline-on-hover"