- Home /
Taking a screenshot from a secondary off-screen camera?
I have been trying create a system for an iOS game that will generate a random photo that users can then share over social media via the Prime31 Social Networking plugin. I am creating the image that I would like for users to be able to share using various Gameobjects. They are all being rendered off-screen but while the game is running.
It seems like I should be able to do this very simply, however I am struggling to find a way to generate a PNG out of my off-screen content. I used the little documentation available on this topic to piece together the attached script. As far as I have seen, everyone is focused on taking screenshots of content that is in view of the main camera and therefore also to the user.
My script currently crashes Unity, I know this is caused by the invalid negative parameter for the new rectangle I am trying to create off-screen, but I wanted to illustrate what I am trying to do.
So to sum up my questions include, is it even possible to convert my off-screen content to a PNG? How would I go about generating a PNG from the content if it is possible? Is my current method the best way to capture that content and package it for use with the Prime31 plug-in?
Other notes, I don't have Unity Pro so the RenderTexture method of taking screenshots is out of question, even though I'm not sure if it could be used to solve my problem.
Attached code:
using UnityEngine;
using System.Collections;
using System.IO;
public class ScreenShot_Taker : MonoBehaviour {
Camera screenshotCam;
RenderTexture renderTex;
int width = 200, height = 200;
void Awake() {
screenshotCam = GetComponent<Camera>();
renderTex = new RenderTexture(width, height, 24);
screenshotCam.targetTexture = renderTex;
}
Texture2D CaptureScreenshot () {
RenderTexture tempRT = RenderTexture.active;
RenderTexture.active = renderTex;
screenshotCam.Render();
Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
screenshot.ReadPixels(new Rect(-38.85f, 5f, width, height), 0, 0);
screenshot.Apply(false);
RenderTexture.active = tempRT;
return screenshot;
}
void SaveScreenshot() {
string filePath = Application.dataPath + "/screenshot.png";
File.WriteAllBytes(filePath, CaptureScreenshot().EncodeToPNG());
}
}
Your answer
Follow this Question
Related Questions
I need to take (save) a screenshot from a camera other than the main one. How would I do this? 0 Answers
Rendering screenshot using multiple cameras and one rendertexture 0 Answers
Can you change ScreenShotCapture Camera? 1 Answer
Taking huge screenshots using camera array? 3 Answers
How do I take a high quality screen shot from my scene view camera? 5 Answers