- save render texture as png
- pngとして画像を保存
using System.IO;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Rendering;
using UnityEngine.UI;
public class RenderTex2PNG : MonoBehaviour
{
[SerializeField] private RenderTexture _renderTex;
private Texture2D _texture;
void Update(){
if(Input.GetKeyDown(KeyCode.A)){
Capture();
}
}
public void Capture(){
if(_texture==null){
_texture = new Texture2D(
_renderTex.width,_renderTex.height,TextureFormat.RGBA32, false
);
}
AsyncGPUReadback.Request(_renderTex, 0, _onRequest);
}
private void _onRequest(AsyncGPUReadbackRequest request){
if (request.hasError)
{
Debug.LogError("Error.");
}
else
{
var data = request.GetData<Color32>();
_texture.LoadRawTextureData(data);
_texture.Apply();
}
var str = System.DateTime.Now.ToString("yyMMddHHmmss");
File.WriteAllBytes(
$"{Application.dataPath}/image"+ str + ".png",
_texture.EncodeToPNG()
);
}
}
参考
kitasenjudesign.hatenablog.com