1
1

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 3 years have passed since last update.

UnityでTexture2Dをpng画像として保存する方法

Last updated at Posted at 2020-10-30

#はじめに
Unityで動的にpng画像を書き出したい時がありますが、必要最低限コードの備忘録です。

#実装について

using UnityEngine;
using System.IO;

public class ExpTex : MonoBehaviour
{
    public Texture2D _targetTexture;

    void Start()
    {
        var storagePath = Application.dataPath + "/" + "test.png";

        //テクスチャの外側を定義
        Texture2D tex = new Texture2D(_targetTexture.width, _targetTexture.height);
        //ピクセル情報を入れる処理
        tex.SetPixels(_targetTexture.GetPixels());
        //エンコード処理
        var png = tex.EncodeToPNG();
        File.WriteAllBytes(storagePath, png);
    }
}

#まとめ
基本はテクスチャの型取りとピクセル情報を入れて後はSystem.IOに書き出してもらえれば良いかと思います。

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?