Current render result:
When I am rendering image in Unity and saving as file(.png), quality of those files are reduced. They become tiny and very poor.
How can I enhance that?
Dimension of the image file is 269*168.
Code to save image :
using UnityEngine;
using System.IO;
using System;
//getty code
public class Capture : MonoBehaviour
{
private static int resWidth = 3840;
private static int resHeight = 2160;
private static GameObject camObj = null;
public static string ScreenShotName()
{
return string.Format("{0}/screenshots/{1}.png",
Application.dataPath,
Util.name_);
}
void Start()
{
camObj = gameObject;
Directory.CreateDirectory(Application.dataPath + "/screenshots");
resHeight=GetComponent<Camera>().pixelHeight;
resWidth=GetComponent<Camera>().pixelWidth;
//Util.everyThingHot = true;
}
public static void TakeHiResShot()
{
try
{
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camObj.GetComponent<Camera>().targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camObj.GetComponent<Camera>().Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camObj.GetComponent<Camera>().targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName();
File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
bytes = null;
}
catch(Exception e)
{
Debug.Log("Error");
}
}
}
Sample:
What I see during saving shot:
It's taking a lot CPU,GPU resources, renders correctly but doesn't give me what I want as a file
height,width
manually, it is in the first lines of theclass
. \$\endgroup\$ – Maifee Ul Asad May 7 at 14:54