Hi!
I'm trying to do a screenshot with a clipped camera but i have an error, "Trying to read pixels out of bounds". So i'm giving bad parameters of where to read the pixels.
example: I have a screen of 1000 pixels and what i want to do is to use 3/4 of camera and the other 1/4 GUI. I think i'm doing ok the clipping but when i want to take the screenshot i have that error.
This is the code:
The clipping:
void Awake()
{
if(tk2dCam == null)
{
tk2dCam = this.GetComponent<tk2dCamera>();
}
int posX = (int)(tk2dFullScreenCam.ScreenExtents.width * screenGUIPercentResize);
float resizeX = tk2dFullScreenCam.ScreenExtents.width * screenCamPercentResize;
float resizeY = tk2dFullScreenCam.ScreenExtents.height;
tk2dCam.viewportRegion = new Vector4(posX,0,resizeX,resizeY);
}
The Screenshot:
public Texture2D TakeScreenShot (Camera camera)
{
#if UNITY_ANDROID || UNITY_IPHONE
Texture2D snapShot = new Texture2D((int)camera.pixelWidth, (int)camera.pixelHeight, TextureFormat.ARGB32, false );
RenderTexture snapShotRT = new RenderTexture(Screen.width, Screen.height , 24, RenderTextureFormat.ARGB32);
RenderTexture.active = snapShotRT;
camera.targetTexture = snapShotRT;
camera.Render();
tk2dCamera cam = camera.GetComponent<tk2dCamera>();
snapShot.ReadPixels( new Rect(cam.ScreenExtents.x, cam.ScreenExtents.y, cam.ScreenExtents.width, cam.ScreenExtents.height), 0, 0, false );
RenderTexture.active = null;
camera.targetTexture = null;
snapShotRT.Release();
snapShotRT = null;
return snapShot;
#endif
return null;
}
i'm missing something?