1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity2D: Make a screenshot of any part of your screen

Posted at

###Tips:

  • set up your camera
    1. Create a camera in your scene
    2. 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);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?