- Home /
Texture2D.ReadPixels unknown error "not inside drawing frame"
I am getting this error when running ReadPixels on a Texture2D object.
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.
UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32, Boolean)
The code was not erroring before so I don't know what's changed (it's a simple script, not much to go wrong). I'm using this to take a snapshot of the screen, which I then fade out to create fading transitions between cameras etc. The strange thing is it still works perfectly (the screen is successfully captured into a Texture2D object), however this error has started showing.
I've tried searching for the whole error and parts of it on Google and I can't find a single result. Can anyone shed some light on this error?
Answer by kmeboe · Oct 02, 2012 at 06:38 PM
Have you tried putting this code into a Coroutine? Perhaps the timing of the screen capture is off, and this is causing the complaint. Give it a shot and let us know if it works.
To do this:
1) Put your screen capture code in a method, and give it the "IEnumerator" return type.
2) At the beginning of the method, add the following line (C#):
yield return new WaitForEndOfFrame();
3) Call your method like this (C#):
StartCoroutine(methodName());
I use it both in a coroutine and not. I don't remember any errors before for either method (since I select "pause game on error" in the console).
And you're getting errors even when it's called using StartCoroutine (using WaitForEndOfFrame)? That is odd indeed.
I tried using "yield return new WaitForEndOfFrame();", that has stopped the error occurring. I wonder why it's only just started. I guess I'll have to change the non-coroutine method to use a coroutine also. Thanks.
Glad it worked! Don't forget to mark the answer as accepted. :)
Same error (and solution) here. I suspect the issue was simply silent before Unity 3.5.6f4.
Answer by ArchyInf · Dec 09, 2012 at 08:53 PM
Here is a script component that should allow capturing a RenderTexture without waiting for the next main rendering to finish. Attach it to your camera and immediately after calling camera.Render(), RenderResult will contain a texture in system memory containing the render result. The works because ReadPixels is executed while the "drawing Frame" of the camera is still active.
using UnityEngine;
public class CameraCapture : MonoBehaviour
{
public Texture2D RenderResult;
void OnPostRender ()
{
Camera owner = GetComponent< Camera >();
RenderTexture target = owner.targetTexture;
if( target == null )
return;
RenderResult = new Texture2D( target.width, target.height, TextureFormat.ARGB32, true );
Rect rect = new Rect( 0, 0, target.width, target.height );
RenderResult.ReadPixels( rect, 0, 0, true );
}
}
Answer by cecarlsen · Nov 16, 2012 at 03:22 PM
I have a method that generates the error above. The method does a on time immediate render of GL graphics to a RenderTexture and onwards to a Texture2D using ReadPixels(). Is this no longer possible? Do I have to set a flag when I need a texture, then wait for the end of the frame to create it and return it to an event handler?
You might have problems if you're trying to immediately grab something using ReadPixels(). In my code, I have the Texture2D creation and ReadPixels() inside a coroutine. Before I perform those steps, I have a "yield return new WaitForEndOfFrame();" at the top of the coroutine to give the rendering a chance to finish.
Thanks. I wonder if it is still possible somehow to render GL to a Texture2D immediately on demand. I created an example on the forum here: http://forum.unity3d.com/threads/158918-Rendering-GL-to-a-Texture2D-immediately-in-Unity4
If anyone has succeeded in rendering GL to a RenderTexture in Unity4, then please view the forum thread just above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get banner ad's screenshot to a texture? 0 Answers
Texture2D.ReadPixels ignores Sprite Mask, captures hidden sprites 0 Answers
Texture2D readPixels: How to only read a section of the camera view? 0 Answers
Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers
Only grey (205) when reading pixels from a RenderTexture to a Texture2D 1 Answer