2D Toolkit Forum

2D Toolkit => Support => Topic started by: gonzalez.martin90 on April 24, 2015, 02:38:34 pm

Title: Screenshot from Clipped Camera
Post by: gonzalez.martin90 on April 24, 2015, 02:38:34 pm
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:

Code: [Select]
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:

Code: [Select]
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?
Title: Re: Screenshot from Clipped Camera
Post by: unikronsoftware on April 28, 2015, 03:21:17 pm
I don't know specifically how to handle this as I've not had any reason to do this before. My guess is that pixelWidth is just the width of the viewport and not the entire screen, but I can't say for certain. You'll need to debug this to find out what doesn't add up