2
\$\begingroup\$

I am searching now for almost an hour and I cannot figure out how correctly to select the background image and position of a visual element.

The query docu is really poor, there is literally nothing:

UQuery is a set of extension methods allowing you to select individual or collection of visualElements inside a complex hierarchy.

All the youtube guys show the same example with a Button.

I have this structure:

https://imgur.com/p6qrVbP

Each "Layer" contains a background image:

enter image description here

Here is my code:

    public class ParallaxController : MonoBehaviour {
 
    public VisualElement layer_0;
    public Image layer_0_0;
    public Image layer_1;
    public Image layer_1_1;
    public Image layer_2;
    public Image layer_2_2;
    public Image layer_3;
    public Image layer_3_3;
    public Image layer_4;
    public Image layer_4_4;
 
    // Start is called before the first frame update
    void Start() {
        var root = GetComponent<UIDocument>().rootVisualElement;
 
        layer_0 = root.Q<VisualElement>("Layer_0");
        Debug.Log(layer_0.style.backgroundImage);
        Debug.Log(layer_0.style.backgroundImage.value.sprite);
        Debug.Log(layer_0.style.backgroundImage.value.texture);
        Debug.Log(layer_0.style.position);
        //Debug.Log(layer_0.Q.<Image>());
        //Debug.Log(layer_0.Q.<Background>());
    }
 
    // Update is called once per frame
    void Update() {
     
    }
}

Its always Null

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can change the background image in this way:

public Texture2D ExampleTexture;

void Start()
{
    var layer_0 = root.Q<VisualElement>("Layer_0");
    layer_0.style.backgroundImage = ExampleTexture;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .