Unity通过WWW 类从persistentDataPath(或从网络)加载图片之内存暴增的问题

  • 问题描述

      在游戏中过程中,我们可能需要动态的加载某一些图片。这些图片不一定要在build的时候放在包中,那么就需要在可读可写的路径persistentDataPath下加载,或者给一个网络地址(URL)下载。无论采取哪种,需要用到Unity的WWW类来获取。

      值的高兴的是,www有一个关于如何获取下载的图片的API:www.texture字段和www.LoadImageIntoTexture(Texture2D)方法。所以如果不考虑其他未知的问题,以上两个获取图片的方式都可以满足我们的需求。但是仔细想想,既然这两个方式都是殊途同归,那么Unity为什么这样做呢?

     在我的项目中,我就入了这个坑。我是通过www.texture的方式获取下载的图片。那么问题来了,当我下载并加载到内存中的图片张数超过60时,Unity的Texture内存增加到550M,如果继续加载,内存依次增加,直至手机内存不足而崩溃退出!!!!!

  • 两种加载方式的分析
  1. www.texture

      官方说明:Returns a Texture2D generated from the downloaded data (Read Only).保存了下载后的图片,以Unity中的Texture2D格式返回给调用者。

      如果下载一张图片,用www.texture的方式获取,在使用了一段时间后,将该图片Destroy掉,但在内存中该图仍然存在,

各位请看加载图片的代码:

public IEnumerator LoadTexture(string name,RawImage spriteReceiver, System.Action onLoadedSuccess = null, System.Action onLoadedFailed = null)
{
    if(isLoaded(name))
    {
        yield break;
        // use loaded texture
    }
    string url = "file:///" + Application.persistentDataPath + "/" + name + ".png";
    WWW www = new WWW(url);
    while (!www.isDone)
    {
        yield return null;
    }
    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(name + www.error);
        // download from web service
    }else
    {
        Debug.Log("load from local");
        Texture2D t2d = new Texture2D(1, 1);
        LoadedSprite lsp = new LoadedSprite(topicId_index, t2d);
        GraySprites.Enqueue(lsp);
        t2d = www.texture;
        spriteReceiver.texture = t2d;
        spriteReceiver.texture.name = name;
        onLoadedSuccess?.Invoke();
    }
    www.Dispose();
    www = null;
    trim(GraySprites);
}
private void trim(Queue<LoadedSprite> queue)
{
    if (queue.Count > 60)
    {
        LoadedSprite sp = queue.Dequeue();
        DestroyImmediate(sp.texture);
    }
}

 问题是,数量大于60后,内存没有随着DestroyImmediate(texture)的调用而保持平稳水平,而是一直增加。

                                        

详细阅读代码,发现这四句代码是问题的关键,并将代码:t2d = www.texture;紧随放在new Texture(1,1);之后,问题即得到解决,内存在数量大于60之后维持在一定水平不再增加。

修改后的代码如下:

public IEnumerator LoadTexture(string name,RawImage spriteReceiver, System.Action onLoadedSuccess = null, System.Action onLoadedFailed = null)
{
    if(isLoaded(name))
    {
        yield break;
        // use loaded texture
    }
    string url = "file:///" + Application.persistentDataPath + "/" + name + ".png";
    WWW www = new WWW(url);
    while (!www.isDone)
    {
        yield return null;
    }
    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(name + www.error);
        // download from web service
    }else
    {
        Debug.Log("load from local");
        Texture2D t2d = new Texture2D(1, 1);
        t2d = www.texture;
        LoadedSprite lsp = new LoadedSprite(topicId_index, t2d);
        GraySprites.Enqueue(lsp);
        spriteReceiver.texture = t2d;
        spriteReceiver.texture.name = name;
        onLoadedSuccess?.Invoke();
    }
    www.Dispose();
    www = null;
    trim(GraySprites);
}
private void trim(Queue<LoadedSprite> queue)
{
    if (queue.Count > 60)
    {
        LoadedSprite sp = queue.Dequeue();
        DestroyImmediate(sp.texture);
    }
}

     2.www.LoadImageIntoTexture(Texture2d)

      官方说明:Replaces the contents of an existing Texture2D with an image from the downloaded data.将下载后的图片数据写到一张已存在的Texture2d 图片中,这张图片原来的数据会被覆盖。

通过这种方式加载图片的代码如下:

public IEnumerator LoadTexture(string name,RawImage spriteReceiver, System.Action onLoadedSuccess = null, System.Action onLoadedFailed = null)
{
    if(isLoaded(name))
    {
        yield break;
        // use loaded texture
    }
    string url = "file:///" + Application.persistentDataPath + "/" + name + ".png";
    WWW www = new WWW(url);
    while (!www.isDone)
    {
        yield return null;
    }
    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(name + www.error);
        // download from web service
    }else
    {
        Debug.Log("load from local");
        Texture2D t2d = new Texture2D(1, 1);
        LoadedSprite lsp = new LoadedSprite(topicId_index, t2d);
        www.LoadImageIntoTexture(t2d);
        GraySprites.Enqueue(lsp);
        spriteReceiver.texture = t2d;
        spriteReceiver.texture.name = name;
        onLoadedSuccess?.Invoke();
    }
    www.Dispose();
    www = null;
    trim(GraySprites);
}
private void trim(Queue<LoadedSprite> queue)
{
    if (queue.Count > 60)
    {
        LoadedSprite sp = queue.Dequeue();
        DestroyImmediate(sp.texture);
    }
}

这里,变量t2d是在www.LoadImageIntoTexture(t2d);这里赋的值。运行这段代码,加载数量大于60之后,内存维持在一定水平而不再增加。

    一开始,我以为是我写代码不注意,对变量t2d的申明、赋值和使用顺序有问题(事实却是如此,t2d = www.texture;方式必须要要先赋值,再使用;但是www.LoadImageIntoTexture(t2d);方式却并没有这种限制,我们可以在这段代码之前,将t2d变量作为引用类型进行使用是没问题的),仔细思考,既然t2d是引用类型,那么t2d = www.texture;出现的顺序和www.LoadImageIntoTexture(t2d);一样,但是得到的结果不一样:前者并没有将Texture保存到列表中,成为了UnUsedAssets,失去了引用,要想释放内存,只能手动调用Resources.UnloadUnusedAssets();;而后者却将Texture放在t2d中并保存到列表中。

  • 问题的讨论

      个人认为,还是因为自己写代码的不规范导致的问题,如果按照正常的逻辑写,是不会出现这个问题的。但是顺序变换了,在我们的思维中,逻辑也是没问题的,但是,Unity中,关于t2d = www.texture和www.LoadImageIntoTexture(t2d);的理解是不一样的。LoadedSprite lsp = new LoadedSprite(topicId_index, www.texture);这样的代码是没有问题的,问题出在了中间多了一个t2d变量。(绕死我了!!!!!)如果您对此有更好的看法,欢迎不吝赐教。

     最后,Unity中一张Texture的内存占用是比较大的,如果在使用动态读取加载图片,那么一定要注意这方面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章