Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Dynamicly change a background sprite of a custom VisualElement

Discussion in 'UI Toolkit' started by Magnilum, Sep 12, 2023.

  1. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    I am doing a toggle button with an icon inside. I am using the Basefield<bool> to inherit my class to benefit of the RegisterValueChangedCallback.

    The only problem is, I would like to use this new toggle and change the sprite of the background but as it is a VisualElement of custom control, everything is gray and cannot be changed.

    Do I need to use a class of is there an other way?
     
  2. laila-chammaa

    laila-chammaa

    Unity Technologies

    Joined:
    May 10, 2022
    Posts:
    28
    Hello!

    Yeah, the team is aware that editing the internal structure of controls can be a weird workflow in the builder. The elements are disabled because they are added through code. These elements are considered implementation details. You will need a class to target that element and you can edit the background through it.

    Hope that makes sense!
     
  3. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    Thank you for your answer.

    It would be easier to access to the VisualElement even for other application like changing the structure of an visualElement already existing to apply fancy uss of modifications.

    For now I am using the uss to change the background image without using code since it can be such a pain to reference the guid of the sprite or make a database or something else.