• 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
Question by JKLs · Dec 12, 2011 at 11:17 AM · texture2dscreenshotthread

How do I use Texture2d in other thread?

I made ScreenShot function in my app, and actually it works. But in this function, if the screen resolution is large, this inserts a little delay (within one second). So that I tried to use System.Threading.Thread() and call GenerateScreenShot() in thread. But faced below error related to Texture2D.

So are there other solution in unity? My app uses USB camera, and delay isn't allowed. Please let me know...

 void GenerateScreenShot(string filename_) {
     int sw = Screen.width;
     int sh = Screen.height;
     Texture2D tex = new Texture2D(sw, sh, TextureFormat.RGB24, false);
     tex.ReadPixels(new Rect(0.0f, 0.0f, sw, sh), 0, 0);
     tex.Apply();
     byte[] bytes = tex.EncodeToPNG();
     Destroy(tex);
     Debug.Log(Application.dataPath);
     string url =  Application.dataPath.ToString() + filename_;
     File.WriteAllBytes(url, bytes);
 }

error in thread

 UnityEngine.Texture2D:Internal_Create(Texture2D, Int32, Int32, TextureFormat, Boolean)
 UnityEngine.Texture2D:.ctor(Int32, Int32, TextureFormat, Boolean) (at C:\BuildAgent\work\842f9557127e852\Runtime\ExportGenerated\Editor\Graphics.cs:506)
 MyScript:GenerateScreenShot(String) (at Assets\Scripts\MyScript.cs:102)
 MyScript:start() (at Assets\Scripts\MyScript.cs:156)
Comment
syclamoth
MaxLohMusic

People who like this

2 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 Thirdblood · Mar 13, 2012 at 05:46 PM 0
Share

I am having this same issue with delay. Were you able to figure it out with threading?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Meltdown · Mar 13, 2012 at 06:18 PM

You can call thread safe .NET methods from your code, but unfortunately Unity doesn't allow you to multi-thread its own internal methods.. such as Texture2D.ReadPixels(), which is probably why you have that error.

Comment

People who like this

0 Show 0 · 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

Answer by Rabbit-Stew-dio · Mar 13, 2012 at 08:12 PM

ReadPixels copies rendered image data from the graphics buffer into the texture. This can only happen on the render-thread (as OpenGL, and probably DirectX as well, are inherently single threaded) and it can only be done between two frames.

If you are lucky, you should be able to move the "EncodeToPNG" method to a different thread, but never the "ReadPixels" call.

Alternatively, you may be able to use Application.CaptureScreenshot instead.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

What's wrong with my screenshot function? 1 Answer

RenderTexture to Texture2D 4 Answers

Capture screen of other spot 1 Answer

add text to a Texture2D while game is running 2 Answers

Capture screenshot to texture, memory issue 1 Answer


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