0
votes

i am working on this game and the image is displayed fine in the game screen in Unity editor but when i build and run the image is blank. Game view in unity: !(Game view in unity) the view after building the texture source is the hard drive. the image is not in the project folder and is not built Any suggestions? Please note i am learning unity and CSharp(New user).

Button Code:

public Category category;
public TextMeshProUGUI text;
public RawImage image;
public bool isFilled = false;
public int directoryLineNumber;

public void UpdateButtonData()
{
    text.text = category.name;
    image.texture = category.GetARandomImage();
    //image.Rebuild(0);
    isFilled = true;
}
{
    public string name;
    public string text;
    public List<Item> items;
    private string _path;
    //some code here not related
public Texture2D GetARandomImage()
    {
        DirectoryInfo dir = new DirectoryInfo(_path);
        FileSystemInfo[] images = dir.GetFileSystemInfos();
        int i = Random.Range(0, images.Length);
        if (images[i].ToString().Contains(".png"))
        {
            WWW wWW = new WWW(images[i].ToString());
            /* Wait !! */
            while (!wWW.isDone) { }
            return wWW.texture;
        }
        else return GetARandomImage();
    } 
1
This is a common problem thant can come from many places. Try to remove all scenes from Build and add it back in.Tiago Martins Peres 李大仁
Now i have tried to : - remove and re attach the scene - uninstall and reinstall Unity - add the texture from the editor ( it worked but the method is not good for the game ) - i have used a different PC and it is the same please any suggestions i am happy to hear even if it is just restarting my pc Lol.Mohamed Shiha
If using the texture from the editor worked, why not use it? I assume it's due to performance issues. If that's the case, I've written to another user how to proceed to get an app fasterTiago Martins Peres 李大仁

1 Answers

0
votes

Hi All i have solved this issue by usingTextuer2d.LoadImage(wWW.bytes); instead of wWW.tetxure.

And now i can use different images types like '.JPG' and '.JPEG' The code now looks like this:

public Texture2D GetARandomImage()
{
    DirectoryInfo dir = new DirectoryInfo(_path);
    FileSystemInfo[] images = dir.GetFileSystemInfos();
    int i = Random.Range(0, images.Length);
    WWW wWW = new WWW(images[i].ToString());
    /* Wait !! */
    while (!wWW.isDone) { }
    Texture2D tex = new Texture2D(1, 1);
    tex.LoadImage(wWW.bytes);
    return tex;
}

Thanks @Tiago for your suggestions.