2
4

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.

[Unity] テクスチャの名前をいれておくとMemoryProfilerでみたときに画像が表示されて良い

Last updated at Posted at 2018-06-20

AB化されていない画像を読み込んでも MemoryProfiler上で表示してほしい

AssetBunle 化したテクスチャや、 Resource.Load を使って読み込んだテクスチャは次のようにMemoryProfiler上からどのテクスチャなのか確認できる。

Screen Shot 2018-06-20 at 22.34.26.png

だけど、画像はそのままのファイルのままで扱うこともできるので、アセットバンドル化せずに扱ったりもする。

// png画像を読み込んでTexture2Dにする
var texture = LoadTexture(filePath);
image.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), Vector2.zero);

そのときに読み込んだ画像が MemoryProfiler 上では次のようになって、どの画像なのかわからなくなるのでちょっと不便。

Screen Shot 2018-06-20 at 23.06.55.png

これはテクスチャを生成したときに名前を入れてあげると解決した。

// png画像を読み込んでTexture2Dにする
var texture = LoadTexture(filePath);
texture.name = filePath;
image.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), Vector2.zero);
Screen Shot 2018-06-20 at 23.11.30.png

MemoryProfier側の実装をみてみる

Inspector.cs の GetTexture(NativeUnityEngineObject nativeObject) で以下の処理をしていた。

if(loadedTextures[i].name == nativeObject.name)
{
    return loadedTextures[i];
}

テクスチャの名前で比較している感じ。

昔のMemoryProfilerなら名前いれなくてもみれたけどなと思って確認したら、

EditorUtility.InstanceIDToObject(nativeObject.instanceID) as Texture2D;

をしてテクスチャをとっていた。最新のものと実装が変わっていたのか。

まとめ

そのままの画像ファイルを Unity で使う場合は Texture2D 生成時に name を更新しておくと MemoryProfiler が表示してくれるので確認しやすくなる。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?