Categories
Development Unity

How to take Screen Shot in Particular Area in unity

i am using unity 2018. In my project i have to take particular area screen. I have using the below code. It is working. But The exact image is not working. It goes some extent. How can i take the exact image.

   using UnityEngine;
   using System.Collections;
   using System;

  public class ScreenCapture : MonoBehaviour
  {
  public RenderTexture overviewTexture;
  GameObject OVcamera;
  public string path = "";

  void Start()
  {
  OVcamera = GameObject.FindGameObjectWithTag("OverviewCamera");
  }
  void LateUpdate()
 {           
 if (Input.GetKeyDown("f9"))
 {
 StartCoroutine(TakeScreenShot());
 }    
 }
// return file name
 string fileName(int width, int height)
{
 return string.Format("screen_{0}x{1}_{2}.png",
 width, height,
 System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
 }

 public IEnumerator TakeScreenShot()
 {
 yield return new WaitForEndOfFrame();

 Camera camOV = OVcamera.camera;  
 RenderTexture currentRT = RenderTexture.active;    
 RenderTexture.active = camOV.targetTexture;
 camOV.Render();
 Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, 
 TextureFormat.RGB24, false);
 imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 
 0);
 imageOverview.Apply();
 RenderTexture.active = currentRT;           
 byte[] bytes = imageOverview.EncodeToPNG();

     // save in memory
 string filename = fileName(Convert.ToInt32(imageOverview.width), 
 Convert.ToInt32(imageOverview.height));
 path = Application.persistentDataPath + "/Snapshots/" + filename;       
 System.IO.File.WriteAllBytes(path, bytes);
 }
 }

this is my above code..

Leave a Reply

Your email address will not be published. Required fields are marked *