0
\$\begingroup\$

So i managed to make a working script that downloads images from firebase in this situation and saves them to Application.persistentDataPath. Now what i would like help in doing is getting all those images "*jpg" and assigning them to a Texture2D array for later use.

So far i have mananged to open one of those images by name and assign it to a single texture.

    public Texture2D image;

 private void Awake()
    {
        string dirname = "levelNature";
        string path = Application.persistentDataPath + "/" + dirname + "/" + "nameofImage.jpg";

        LoadSprite(fileName);
       
    }

    private void LoadSprite(string path)
    {
        if (string.IsNullOrEmpty(path)) 
            Debug.Log("null or empty");

        if (File.Exists(path))
        {
            byte[] bytes = File.ReadAllBytes(path);
            Texture2D texture = new Texture2D(1024, 1024, TextureFormat.RGB24, false);
            texture.LoadImage(bytes);
            image = texture;
            Debug.Log("success");
        }
    }

With just one image i can assign it to a texture but since later on in the gameplay i'm using a texture2d array to assign those images to the gameplay , which now are set through the inspector , i would like to load all images in a particular folder to a Texture2D array which later on i will be using to show each image index in a specific level.

I did try it out like this but it isn't successful.

    public Texture2D[] source;

        var fileNames = Directory.GetFiles(Application.persistentDataPath + "/" + dirname + "/" + "*.jpg");

        source = new Texture2D[fileNames.Length];
        int index = 0;
        foreach (var fileName in fileNames)
        {
            source[index] = LoadSprite(fileName); // i get the error here that it cant convert void to texture2D
            index++;
        }

Hopefully once all images are loaded i could see in that array in the inspector all the images and then use that for the rest of the game. Thank you!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You declared your LoadSprite method as a void method, meaning it never returns any value to the caller.

But then you tried to use it like it was going to give a return value:

source[index] = LoadSprite(fileName);

This says "Take the return value from LoadSprite and assign it to my source array in slot index". But LoadSprite doesn't ever provide a return value, so this is nonsense, and your compiler is rightly telling you that you've written nonsense it can't do anything useful with.

It looks like you wanted to write a method something like this:

private Texture2D LoadTextureFromPath(string path)
{
    if (string.IsNullOrEmpty(path)) {
        Debug.LogError("Texture path is null or empty");
        return null;
    }

    if (!File.Exists(path))
    {
        Debug.LogError($"File not found: {path}");
        return null;
    }

    byte[] bytes = File.ReadAllBytes(path);
    Texture2D texture = new Texture2D(1024, 1024, TextureFormat.RGB24, false);
    texture.LoadImage(bytes);
    Debug.Log("success");

    return texture;
}

See how the method now has type Texture2D, not void, and we actually return a texture at the end? Now this line makes sense:

source[index] = LoadTextureFromPath(fileName);
\$\endgroup\$
2
  • \$\begingroup\$ you couldn't be more correct, im a bit ashamed for not noticing it and thank you! \$\endgroup\$
    – stratos la
    Commented May 4, 2021 at 13:39
  • \$\begingroup\$ Pay attention to your compiler messages and they'll help you spot issues like this faster. The only place "void" shows up is in method signatures, so checking that would have led you straight to the source of the problem. \$\endgroup\$
    – DMGregory
    Commented May 4, 2021 at 16:54

You must log in to answer this question.

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