Hey guys,
The idea, is to render the camera to a RenderTexture, then ReadPixels to a Texture2D, then pause the game, and display that Texture2D for the background of my menu.
I tried to put that texture to a png for debugging purpose, the image I get in the png is exactly what I want, but when it comes to drawtexture it, I have a grey/white image…
Any Ideas?
Here is some code:
private bool IsPaused;
public Texture2D t2d;
void OnGUI()
{
if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
}
Texture2D SaveBackground()
{
int resWidth = 1920;
int resHeight = 1080;
Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera0.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera0.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camera0.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
return screenShot;
}
void PauseGame()
{
t2d = SaveBackground();
////////////texture debugger/////////////
/*byte[] bytes = t2d.EncodeToPNG();
string filename = "testpause.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
////////////////////////////////////////////
IsPaused = true;
}
PS: the code is cut just to have the interesting part, other parts of the code is about disabling gameobject etc…