简体   繁体   中英

Unity CSharp 2D texture

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();
    } 

Hi All i have solved this issue by using Textuer2d.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM