- Home /
resize screenshot
I am taking a screenshot in my iPad game with
Application.CaptureScreenshot( screenshotFilename );
this is later uploaded to social media sites. Works fine on older iPads but the uploads time out on an iPad 4, which I assume is due to the retina screen and the larger screenshot file size. So, how can I overwrite the screenshot with a png that is 1024x768?
Answer by DavidDebnar · Jul 09, 2013 at 05:35 PM
Use the javascript version of the TextureScale plugin. Just copy the code and paste it into TextureScale.js anywhere in your project.
TakeScreenShot method:
import System.IO;
function TakeScreenShot (name : String) {
yield WaitForEndOfFrame(); // this also captures gui, remove if you don't wanna capture gui
var shot = new Texture2D(Screen.width,Screen.height,TextureFormat.RGB24,false);
shot.ReadPixels(Rect(0,0,Screen.width,Screen.height), 0, 0, false);
if(shot.width > 1024 || shot.height > 768)
TextureScale.Bilinear(shot,1024,(1024.0/parseFloat(Screen.width))*Screen.height);
//shot.Apply(); //this is unecessary, if you don't wanna use the image afterwards
var bytes = shot.EncodeToPNG();
var file = new File.Open(Application.persistentDataPath + "/" + name + ".png", FileMode.Create);
var binaryWriter = new BinaryWriter(file);
binaryWriter.Write(bytes);
file.Close();
}
Usage
function Start () {
TakeScreenShot("screenShot"+System.DateTime.Now.ToString("-MM-dd-yyyy--hh-mm-ss"));
Debug.Log(Application.persistentDataPath);
}
thanks - this looks very promising. I can post the image to social media now, but the image just contains a solid block of colour. Any idea what could be causing this?
Yes. Just uncomment //shot.Apply() and it should work.
just tried the js version of the TextureScale class and it works! Thanks so much.
PS Apply() wasn't required
PPS import System.IO; required in script that has TakeScreenShot function
Answer by Jamora · Jul 09, 2013 at 04:50 PM
I haven't tried this personally, so it could scale the image, or just clip it. First, take the screenshot, then load it like explained here http://answers.unity3d.com/questions/14587/load-texture-at-run-time-from-file.html . Finally, use Texture2D.Resize() http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.Resize.html.
thank you. Going to try $$anonymous$$'s first and will try this if I can't get it going.
Texture2D.Resize returns an undefined image, so it won't work.