My game has a feature where it takes a screenshot (Texture2D) and posts it to social media platforms - all pretty standard. I’d like to add some text to the screenshot (score etc) but I’m stuck. Any tips?
Thanks
I ended up doing it with a RenderTexture. Here were the steps I took.
- Duplicate the main game camera, call it “ScreenshotCam” and set it to a lower depth (not sure if that was required)
- Make a new render texture in the project panel and set it to a decent size
- Make the render texture the “target Texture” of the ScreenshotCam
- Add some UI text for score and a UI image for the game logo. Put these both on a layer that is only rendered by the ScreenshotCam (ie uncheck the layer in the culling mask dropdown of the main camera)
- output the current score to the UI text box as the game is being player
- Make the render texture created in step 2 active while taking the screenshot.
Here is the capture function I’m using.
function CaptureScreen() {
yield WaitForEndOfFrame();
var currentRT = RenderTexture.active;
RenderTexture.active = scoreRenderTexture;
var tex = new Texture2D(screenshotScoreCamera.pixelWidth, screenshotScoreCamera.pixelHeight, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(screenshotScoreCamera.pixelRect, 0, 0);
tex.Apply();
RenderTexture.active = currentRT;
return tex;
}
Hope others find this useful.
This should help you, but in general looking into the Texture2D documentation gives you an idea of how to modify a texture (by GetPixels, SetPixels and Apply):
Adding Text-To-Texture at Runtime in Unity3D Without Using Render Texture