从 PersistenceDataPath 加载图像

Sil*_*lva 2 c# image unity-game-engine

我正在制作一个 Android 应用程序,一开始应该从 MySQL 数据库加载数据并从服务器加载一些图像。之后,用户将在外面,因此应用程序将离线工作。

我编写了代码来保存所有数据库数据(它有效)和图像从服务器到 ApplicationPersistenceDataPath (不工作)。我已经在寻找我应该如何做到这一点。我的代码不会引发任何错误。但是,当我尝试在应用程序上显示它时,图像是空白的。当时我查看了存储图像的文件夹,但无法打开它们。

这是我保存图像的方法:

IEnumerator loadBgImage(string url, string file_name)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            yield return www.Send();
            if (www.isNetworkError || www.isHttpError)
            {
                print("erro");
            }
            else
            {
                string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name);
                if (!File.Exists(savePath))
                {
                    System.IO.File.WriteAllText(savePath, www.downloadHandler.text);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是在图像中显示图像文件的代码:

string path = Application.persistentDataPath + "/" + nomeImagem;
imagem.GetComponent<SpriteRenderer>().sprite = LoadSprite(path);
Run Code Online (Sandbox Code Playgroud)

这个方法被称为:

    private Sprite LoadSprite(string path)
    {
        if (string.IsNullOrEmpty(path)) return null;
        if (System.IO.File.Exists(path))
        {            
            byte[] bytes = File.ReadAllBytes(path);
            Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);
            texture.filterMode = FilterMode.Trilinear;
            texture.LoadImage(bytes);
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);

        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

我确实需要先保存图像,然后离线显示。有人可以告诉我我做错了什么吗?

更新 按照@derHugo的答案的建议后,这是我必须保存图像的代码:

IEnumerator loadBgImage(string url, string file_name)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            yield return www.Send();
            if (www.isNetworkError || www.isHttpError)
            {
                print("erro");
            }
            else
            {
                var savePath = Path.Combine(Application.persistentDataPath, file_name);
                var data = www.downloadHandler.data;
                using (var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(data, 0, data.Length);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

它有效,但运行应用程序时我仍然看不到图像。在显示精灵的方法中,我添加了创建的精灵的返回。

第二次更新

我再次遵循@derHugo的建议,并使用rawImage而不是Image。现在它可以在 Unity 编辑器和 Android 设备上运行。这是我使用的方法:

private void LoadSprite(string path)
    {
        if (string.IsNullOrEmpty(path)) print("erro");
        if (System.IO.File.Exists(path))
        {
            byte[] bytes = File.ReadAllBytes(path);
            Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);
            texture.filterMode = FilterMode.Trilinear;
            texture.LoadImage(bytes);
            imagem.texture = texture;
        }
    }
Run Code Online (Sandbox Code Playgroud)

der*_*ugo 5

一般来说你应该使用Path.Combine类似

var path = Path.Combine(Application.persistentDataPath, FileName);
Run Code Online (Sandbox Code Playgroud)

改为系统路径!可能是您的目标设备根本不理解/路径分隔符。


如果文件已经存在(也许是较新的版本?并且您已经下载了它),或者如果文件已经存在,您甚至不应该开始下载以节省带宽。


我还认为这WriteAllText对于二进制图像数据来说并不是完全正确的解决方案,因为www.downloadHandler.text已经

解释为 UTF8 字符串

因此,由于图像很可能有一些无法用 UTF8 字符串表示的字节,因此您会在这里得到损坏的数据!

你宁愿直接使用

www.downloadHandler.data
Run Code Online (Sandbox Code Playgroud)

它返回原始数据byte[],而不是例如(仅在 UWP 上)或将其写入文件File.WriteAllBytes的正确值。就像是FileStreambyte[]

var data = www.downloadHandler.data;
File.WriteAllBytes(path, data);
Run Code Online (Sandbox Code Playgroud)

或者

var data = www.downloadHandler.data;
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
    fs.Write(data, 0, data.Length);
}
Run Code Online (Sandbox Code Playgroud)

场景中的白色图像通常意味着精灵是null

如果没有拼写错误,那么您LoadSprite 总是会返回null。你忘记归还创建的sprite

private Sprite LoadSprite(string path)
{
    if (string.IsNullOrEmpty(path)) return null;
    if (System.IO.File.Exists(path))
    {            
        byte[] bytes = File.ReadAllBytes(path);
        Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);
        texture.filterMode = FilterMode.Trilinear;
        texture.LoadImage(bytes);
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);

        // You should return the sprite here!
        return sprite;
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

但请注意,实际上还有一种加载本地图像的更好方法UnityWebRequestTexture。它还将本地文件路径作为 URL 并

与下载原始字节并在脚本中手动创建纹理相比,使用此类可显着减少内存重新分配。此外,纹理转换将在工作线程上执行。

但是也

仅支持 JPG 和 PNG 格式。

但这不应该是一个问题,因为LoadImage您到目前为止使用的也存在相同的限制。

然而,这将是异步的,因此您必须等待纹理才能创建精灵。因此,Sprite我实际上宁愿传递相应组件的引用Image并直接替换其sprite. 就像是

private IEnumerator LoadLocalTexture(string path, Image receivingImage)
{
    UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) 
    {
        Debug.Log(www.error);
    }
    else 
    {
        var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
        var sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);

        receivingImage.sprite = sprite;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您不一定要实际使用,Sprites我总是建议您使用RawImage可以直接使用的组件Texture2D!而不是做类似的事情

private IEnumerator LoadLocalTexture(string path, RawImage receivingImage)
{
    UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) 
    {
        Debug.Log(www.error);
    }
    else 
    {
        var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;

        receivingImage.texture = texture;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是也

注意:请记住,使用 RawImage 会为每个存在的 RawImage 创建额外的绘制调用,因此最好仅将其用于背景或临时可见图形。