You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

49 lines
1.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveImage : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public RenderTexture renderTexture;
public string fileName = "output.png";
public void save()
{
string timestamp = System.DateTime.Now.ToString("yyyyMMdd_HHmmss");
string fileNameWithTimestamp = System.IO.Path.GetFileNameWithoutExtension(fileName) + "_" + timestamp + System.IO.Path.GetExtension(fileName);
SaveRenderTextureToPNG(renderTexture, fileNameWithTimestamp);
Debug.Log("Image saved to " + fileNameWithTimestamp);
// Optionally, you can also log the full path
string fullPath = System.IO.Path.Combine(Application.persistentDataPath, fileNameWithTimestamp);
Debug.Log("Full path: " + fullPath);
}
void SaveRenderTextureToPNG(RenderTexture rt, string filePath)
{
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(filePath, bytes);
RenderTexture.active = currentRT;
Destroy(tex);
}
}