Hi all,
in my Unity application, I want to capture the screenshot. So I use following code (as a plugin for Unity)
+(void)shot:(id)sender
{
CGRect rect = [[UIScreen mainScreen] bounds];
int width = rect.size.width;
int height = rect.size.height;
NSInteger dataLength = width * height * 4;
GLubyte *buffer = (GLubyte *) malloc(dataLength);
GLubyte *buffer2 = (GLubyte *) malloc(dataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
buffer2[((height - 1 - y) * width * 4 + x)] = buffer[(y * 4 * width + x)];
}
}
free(buffer);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, dataLength, releaseData);
int bitsPerComponent = 8;
int bitsperPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsperPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
Everything works ok. Only that the screenshot picture is whole black(my scene has contents of course). I googled to find a similar question here:
but since it is for Cocos2D, not Unity, I don’t know where to apply the solution to my Unity project. Need your help, please!
PS.
Unity version 4.0, ios version 6.1.