I’ve come to a point where my code works when compiled and run as standalone, but not when running within the editor.
I was wondering if there is something wrong in my code, or a limitation of some sort that I missed maybe.
Using the following code will work when compiled, but not in the editor.
It’s not a problem, as long as you know it does work in the end…
function TakePreview () {
// creating a Texture to Render to
var myRenderTexture = new RenderTexture(128, 96, 24);
myRenderTexture.Create();
// Setting the Camera to Render to the Texture
Camera.main.targetTexture = myRenderTexture;
// Setting the Texture to be the Active Texture to read from
RenderTexture.active = myRenderTexture;
// Creating a new Texture2D
var myTexture2D = new Texture2D (128, 96, TextureFormat.RGB24, false);
// Wait for the Frame to be fully rendered
yield WaitForEndOfFrame();
// Read pixels from the Render Texture into the Texture 2D
myTexture2D.ReadPixels(new Rect(0, 0, myRenderTexture.width, myRenderTexture.height), 0, 0);
myTexture2D.Apply();
// Reset Camera and Release the RenderTexture
Camera.main.targetTexture = null;
RenderTexture.active = null;
myRenderTexture.Release();
}