Background image on canvas

1

I have a canvas to which I have added a raw image.

How can I get a background image from code?

I have this code that reads some folders and looks for where the image is.

private void LoadBgImage() {
        string RuteBackground = Application.dataPath + "/StreamingAssets/ImgBackground";

        string[] files = Directory.GetFiles(RuteBackground);
        string background = files.FirstOrDefault(s => s.EndsWith(".jpg") || s.EndsWith(".png"));

    }

In background I keep the path of the image.

How do I assign it to the raw image of the canvas?

    
asked by JuanPerez 10.04.2017 в 17:08
source

2 answers

0

For this you can create a panel that occupies the entire canvas, and then from the script you create a variable like this:

Public Image imagenDeFondo;

Then you access the sprite property of the panel and assign it your image.

imagenDeFondo.sprite = tuImagen;

I hope I have helped, Anything comments.

    
answered by 03.05.2017 в 15:04
0

You can get the image from a given route in the following way:

Sprite img = Resources.Load<Sprite>(background);

where background is the path of the image of your example. Then you can add the image to your control.

imagen.sprite = img
    
answered by 09.11.2017 в 20:19