0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DownloadHandlerTextureで取得した画像が解放されない

Last updated at Posted at 2019-11-14

TL;DR

Destroy(texture)またはResources.UnloadUnusedAssets()
またはシーン遷移

DownloadHandlerTexture

DownloadHandlerTextureは画像をAssetBundleにしなくても使うことができます。
つまり、適当なhttps://example.com/yukamaki.jpgみたいなのを拾ってきてTexture2Dにまでしてくれるクラスです。
マニュアルを読む限り、textureプロパティにアクセスすることで追加のアロケーションは発生しないと書いてありますが、デコードはメインスレッドなのか、Rawなデータは破棄されているのかあたりが気になるところです。// 今度調べる
RawデータはHandlerのDisposeで消えそうな気がしますが…

AssetBundleで管理している場合はAssetBundle.UnloadでOKです。

Runtime生成Asset

ランタイム生成したAsset(Object)は基本的にこの扱いになり、DownloadHandlerTexture.textureはRuntime生成となるようです。
他にも、お馴染みInstanciate(prefab)new GameObject()new RenderTexture()などがこれに当たります。

C#のオブジェクトがGCされたら一緒に解放

class ManagedTexture2D : Texture2D
{
    public ManagedTexture2D(int width, int height) : base(width, height){}
    
    // GC時に呼ばれる
    ~ManagedTexture2D() => Destroy(this);
}

DownloadHandlerTextureのように、自分でnewしていないときには成すすべ無いので微妙…

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?