繁体   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