IRCSS

Save RenderTexture as Texture

Nov 4th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1.  void SaveRenderTexture(RenderTexture rd)
  2.     {
  3.         Texture2D screenShot = new Texture2D(rd.width, rd.height, TextureFormat.RGB24, false);
  4.         RenderTexture.active = rd;
  5.         screenShot.ReadPixels(new Rect(0, 0, rd.width, rd.height), 0, 0);
  6.         RenderTexture.active = null;
  7.         byte[] bytes;
  8.         bytes = screenShot.EncodeToJPG();
  9.        
  10.         string nameOfTheImage = rd.name + ".jpg";
  11.         // use   Path.Combine for platform independet / or \
  12.         string pathToTheFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
  13.             "/" + UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + "/" + "set01" + "/";
  14.         if (!Directory.Exists(pathToTheFile))
  15.         {
  16.             Directory.CreateDirectory(pathToTheFile);
  17.         }
  18.  
  19.         string fileName = pathToTheFile + nameOfTheImage;
  20.  
  21.  
  22.         System.IO.File.WriteAllBytes(fileName, bytes);
  23.     }
Add Comment
Please, Sign In to add comment