Graphics.Blitを使ってマテリアルを適用したテクスチャをRenderTextureに描画、読み出せば画像に保存できます。画像サイズ、マテリアルと画像のパスは適当に変えてください。
cs
1using System.IO;
2using UnityEditor;
3using UnityEngine;
4
5public class MaterialToImage
6{
7 private const int width = 512;
8 private const int height = 512;
9 private const string materialPath = "Assets/Unlit_NewUnlitShader.mat";
10 private const string outputPath = "Assets/output.png";
11
12 [MenuItem("Tools/SaveImage")]
13 static void SaveImage()
14 {
15 var texture = Texture2D.whiteTexture;
16 var material = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
17 var renderTexture = RenderTexture.GetTemporary(width, height, 0);
18
19 Graphics.Blit(texture, renderTexture, material);
20
21 RenderTexture.active = renderTexture;
22 var tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
23 tex.ReadPixels(new Rect(0f, 0f, renderTexture.width, renderTexture.height), 0, 0);
24 tex.Apply();
25 var bytes = tex.EncodeToPNG();
26 Object.DestroyImmediate(tex);
27 RenderTexture.ReleaseTemporary(renderTexture);
28
29 File.WriteAllBytes(outputPath, bytes);
30
31 AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceUpdate);
32 }
33}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/11/05 08:13