public static void SaveRTToFile()
{
RenderTexture rt = Selection.activeObject as RenderTexture;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
RenderTexture.active = null;
byte[] bytes;
bytes = tex.EncodeToPNG();
string path = AssetDatabase.GetAssetPath(rt) + ".png";
System.IO.File.WriteAllBytes(path, bytes);
AssetDatabase.ImportAsset(path);
Debug.Log("Saved to " + path);
}
์ฌ๋ฒ... Texture2D ๋ถ๋ชจ ๊ฐ์ฒด ์ค์ RenderTexuter๋ผ๋ ์์???
์ด๋ป๊ฒ RenderTexture์ rt๋ฅผ ๋ถ์๋๋ฐ ์ ๊ฑธ ์ฝ์ด ๋๋ฆฌ๋๊ฒ ๋๋? - _ -
์ ๊ธฐํ๊ฑฐ๋ ์ ๋๋ค์ ์ ๊ฒ ๋๋ค๋ ๊ฑธย ์ด๋ป๊ฒ ์์ ๋ธ๊ฑฐ์ง?
ReadPixels ํจ์๊ฐ ํต์ฌ์ด๋ค
๊ทธ๋๊น. ์๋ฌด๋๋ RenderTexture.active ์คํํฑ์ผ๋ก ๋ฐ์ ๋๊ณ ๊ฑ ์ ๊ทผํ๋ ๋ณธ๋ฐ ๊ทธ๋ฅ ๋ง ์ ๋ ๊ฒ ์ ๊ทผํด๋ ๋๋ ์ถ์ด.
์ง์ง ๊ทผ๋ณธ ์๋๋ฐ ๊ทธ๋ฌ๋ฉด
ํน๋ณํ๊ฑด ์์ด๋ณด์ด๋๋ฐ
ReadPixelsํจ์์ ํฌ์ธํฐ๋ฅผ ์๋๊ฒจ ์ฃผ๋๋ฐ ์ฝ์ด ๋ค์ด์์.
Reads the pixels from the current render target (the screen, or a RenderTexture) ํจ์ ์ํ์ ํฌ์ธํฐ ํ๋ผ๋ฏธํฐ๊ฐ ์๋๋ฐ ์ด๋ป๊ฒ ์ฃผ๋.. RenderTexture.active = rt ๋ฅผ ๋ฃ์ ์๊ฐ ๋์ธ๋ฐ
active ์ ๋์ ํ๋ฉด ์ฑ๊ธ ์ปจํ ์คํธ์ ์ธํ ํ๊ฒ ์ง
active ๋ ์คํํฑ ๋ฉค๋ฒ์๋
์ ์ด์ Readoixels() ์ด๋์ด ์ ํ Rect ๋ฒ์์ ์ค์ท์ ์ฐ์ ์ค์ฅ ์ฐ๋ ๋์์ ํ์ฌ active ๋ RT๋ฅผ ์ฐ๋๊ฑฐ๊ณ . ๊ทธ๋ฌ๋๊น ๊ทธ RT๋ฅผ ์ ์ด์ ๊ทธ ํจ์์์ ์ง์ ํด์คํ์๊ธฐ ์๋จ ๊ฒ. ์ด๋ฏธ ์์์ ์ฐ์ด์ผํ ๋์ด ์ด๋ ๋์ธ์ง RenderTexture.active = rt; ํด์คฌ์ผ๋๊น API ์ ์ฝ์ด๋ด