簡體   English   中英

Unity CSharp 2D 紋理

[英]Unity CSharp 2D texture

我正在開發這款游戲,並且圖像在 Unity 編輯器的游戲屏幕中顯示良好,但是當我構建和運行圖像時,圖像是空白的。 統一的游戲視圖:!(統一的游戲視圖構建紋理源后的視圖是硬盤驅動器。 該圖像不在項目文件夾中並且未構建任何建議? 請注意我正在學習統一和 CSharp(新用戶)。

按鈕代碼:

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

大家好,我已經通過使用Textuer2d.LoadImage(wWW.bytes); 而不是wWW.tetxure

現在我可以使用不同的圖像類型,如 '.JPG' 和 '.JPEG' 代碼現在看起來像這樣:

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

感謝@Tiago 的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM