• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by AliAzin · Dec 07, 2010 at 02:47 PM · texturerendertexture

Is it possible to save RenderTextures into png files for later use?

Hi,

Is it possible to save RenderTextures into png files for later use?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · May 30, 2012 at 07:38 PM 0
Share

try this one for essentially the code needed ...

http://answers.unity3d.com/questions/227280/create-a-texture2d-dynamically-from-a-screenshot-o.html

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Eric5h5 · Dec 07, 2010 at 06:34 PM

Use ReadPixels to read the RenderTexture into a Texture2D and save that using EncodeToPNG.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AliAzin · Dec 08, 2010 at 12:05 PM 0
Share

Thanks for your help. I managed to capture a render texture, but it seems that it is only possible in OnRenderImage(). Is it possible to do it in other methods?

avatar image Eric5h5 · Dec 08, 2010 at 07:00 PM 1
Share

@AliAzin: you should be able to do it with Camera.Render.

avatar image Fattie · Apr 15, 2014 at 02:28 PM 2
Share

here's some random example code, that may be of help to someone.

Starting at "RenderTexture tempRT = ..."

 public void $$anonymous$$akeSquarePngFromOurVirtualThingy()
     {
     // capture the virtuCam and save it as a square PNG.
     
     int sqr = 512;
     
     virtuCamera.camera.aspect = 1.0f;
     // recall that the height is now the "actual" size from now on
     
     RenderTexture tempRT = new RenderTexture(sqr,sqr, 24 );
     // the 24 can be 0,16,24, formats like
     // RenderTextureFormat.Default, ARGB32 etc.
     
     virtuCamera.camera.targetTexture = tempRT;
     virtuCamera.camera.Render();
     
     RenderTexture.active = tempRT;
     Texture2D virtualPhoto =
         new Texture2D(sqr,sqr, TextureFormat.RGB24, false);
     // false, meaning no need for mipmaps
     virtualPhoto.ReadPixels( new Rect(0, 0, sqr,sqr), 0, 0);
     
     RenderTexture.active = null; //can help avoid errors 
     virtuCamera.camera.targetTexture = null;
     // consider ... Destroy(tempRT);
     
     byte[] bytes;
     bytes = virtualPhoto.EncodeToPNG();
     
     System.IO.File.WriteAllBytes(
         OurTempSquareImageLocation(), bytes );
     // virtualCam.SetActive(false); ... no great need for this.
     
     // now use the image, 
     UseFileImageAt( OurTempSquareImageLocation() );
     }
 
 private string OurTempSquareImageLocation()
     {
     string r = Application.persistentDataPath + "/p.png";
     return r;
     }

avatar image ardiawanbagusharisa Fattie · Dec 07, 2017 at 03:38 PM 0
Share
  UseFileImageAt( OurTempSquareImageLocation() ); //what is this? to what I supposed to change?

 
avatar image
6

Answer by Statement · Dec 07, 2010 at 02:53 PM

Interesting question.

You can grab contents of the current render target with Texture2D.ReadPixels

Texture2Ds can generate png data with Texture2D.EncodeToPNG.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by unity_P_rZF6EevSyxyg · Dec 14, 2018 at 01:12 PM

 public RenderTexture rt;    
 // Use this for initialization
 public void SaveTexture () {
     byte[] bytes = toTexture2D(rt).EncodeToPNG();
     System.IO.File.WriteAllBytes("C:/Users/egsha/SavedScreen.png", bytes);
 }
 Texture2D toTexture2D(RenderTexture rTex)
 {
     Texture2D tex = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
     RenderTexture.active = rTex;
     tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
     tex.Apply();
     return tex;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kristijonas · Feb 10 at 12:41 PM 0
Share

don't to forget dispose Texture2D

  public RenderTexture rt;    
  // Use this for initialization
  public void SaveTexture () {
      byte[] bytes = toTexture2D(rt).EncodeToPNG();
      System.IO.File.WriteAllBytes("C:/Users/egsha/SavedScreen.png", bytes);
  }
  Texture2D toTexture2D(RenderTexture rTex)
  {
      Texture2D tex = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
      RenderTexture.active = rTex;
      tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
      tex.Apply();
     Destroy(tex);//prevents memory leak
      return tex;
  }


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Render GUI elements to off screen camera 1 Answer

Painting on Textures at Runtime? 2 Answers

Need help with creating dynamic textures. 1 Answer

Using Texture2DArray as RenderTarget and passing data to fragment shader 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges