###Tips:
- set up your camera
- Create a camera in your scene
- Render Mode of Canvas - Screen Space Camera, choose the camera you just created
- the input of saveImage() is the Gameobject that you want to make screenshot of (e.g. a panel)
public Camera camera;
RenderTexture renderTexture;
public void saveImage (GameObject go) {
float width = Screen.width + go.GetComponent().offsetMax.x - go.GetComponent().offsetMin.x;
float height = Screen.height - go.GetComponent ().offsetMin.y + go.GetComponent ().offsetMax.y;
renderTexture = new RenderTexture (Screen.width, Screen.height, 0);
camera.targetTexture = renderTexture;
camera.Render ();
RenderTexture.active = renderTexture;
Texture2D virtualPhoto =
new Texture2D((int)width, (int)height, TextureFormat.RGB24, false);
// false, meaning no need for mipmaps
virtualPhoto.ReadPixels( new Rect(go.GetComponent().offsetMin.x,
go.GetComponent().offsetMin.y,
width, height), 0, 0);
RenderTexture.active = null; //can help avoid errors
camera.targetTexture = null;
// consider ... Destroy(tempRT);
byte[] bytes;
bytes = virtualPhoto.EncodeToPNG();
//saveToCloud or
//File.WriteAllBytes(Application.dataPath + "/img/SavedScreen.png", bytes);