Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by maxxa05 · Sep 13, 2014 at 04:32 AM · pluginrendertexturesave dataencodetopng

Faster way to encode RenderTexture to png

So, I have an in-game camera the player can use, and I would like to be able to save the images instantly without noticing a lag. So far, I create render a camera into a RenderTexture and I feed it in this extension method:

 public static void SaveToFile(this RenderTexture renderTexture, string name)
 {
     RenderTexture currentActiveRT = RenderTexture.active;
     RenderTexture.active = renderTexture;
     Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
     tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
     var bytes = tex.EncodeToPNG();
     System.IO.File.WriteAllBytes(name, bytes);
     UnityEngine.Object.Destroy(tex);
     RenderTexture.active = currentActiveRT;
 }

It does work, but ReadPixels is slow as hell and it creates a noticeable hiccup. I tried several ways to smooth out the performance spike created by it. First, I tried to call this method from another thread, but it has to be called in the main thread. Then, I tried to use renderTexture.GetNativeTexturePtr() with Marshal.Copy(), but I can't find a way to do it. I guess the only other way is to make a Native plugin, but my knowledge in C/C++ is pretty limited. Anybody has any idea on this?

Comment
Add comment
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

2 Replies

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

Answer by AlexeyDrobyshevsky · Jan 22, 2021 at 12:18 PM

(This method wasn't available back when the question was asked, but it's still on the first page of google so) The modern (2019+) method to do that is to read texture data using AsyncGPUReadback (https://docs.unity3d.com/ScriptReference/Rendering.AsyncGPUReadback.html) - it's not fast, but it does not block the main thread at least; and then encode the data using ImageConversion.EncodeArrayToPNG (https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeArrayToPNG.html) or a similar method. This one blocks, but according to the docs it's thread-safe.

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 Bunny83 · Jan 22, 2021 at 12:33 PM 0
Share

Constructive necro posts which are on topic like this one are welcome :) Since the OP hasn't been seen for 3+ years I accepted this answer for now.

avatar image
3

Answer by yoyo · Sep 13, 2014 at 06:23 AM

Have a look at Application.CaptureScreenshot, I believe it runs asynchronously, and handles all the file I/O for you as well.

More information on this Unity wiki page, which outlines several methods of capturing screenshots.

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

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.

Follow this Question

Answers Answers and Comments

26 People are following this question.

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

Related Questions

Save and load data via Facebook SDK 0 Answers

Using Texture2D.ReadPixels with a RenderTexture that has AntialiasingAsPostEffect 0 Answers

Pass a RenderTexture to a plugin with zero copies 2 Answers

Render to RenderTexture in android/gles (plugin) 0 Answers

Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 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