【问题标题】:Unity CSharp 2D textureUnity CSharp 2D 纹理
【发布时间】:2019-09-22 10:23:21
【问题描述】:

我正在开发这款游戏,并且图像在 Unity 编辑器的游戏屏幕中显示良好,但是当我构建和运行时,图像是空白的。 统一的游戏视图: !(Game view in unity) the view after building 纹理来源是硬盘。 图像不在项目文件夹中且未构建 有什么建议? 请注意我正在学习统一和 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();
    } 

【问题讨论】:

  • 这是一个常见问题,可能来自许多地方。尝试从 Build 中删除所有场景并将其重新添加。
  • 现在我尝试: - 删除并重新附加场景 - 卸载并重新安装 Unity - 从编辑器添加纹理(它有效,但该方法不适合游戏) - 我用过一台不同的电脑,它是一样的,请任何我很高兴听到的建议,即使它只是重新启动我的电脑 Lol。
  • 如果使用编辑器中的纹理有效,为什么不使用它呢?我认为这是由于性能问题。如果是这样,我已经写信给另一个用户how to proceed to get an app faster

标签: c# image unity3d game-engine


【解决方案1】:

大家好,我已经通过使用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 的建议。

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2012-09-28
    • 2011-08-25
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多