Catlard
1
There’s something wrong with the way I’m finding the filepath for the texture that I’m saving in this script–does anybody know what it is?
(WORKING SCRIPT BELOW IN ANSWER!)
Hey there!
No problem. It is a bit hard to figure out. For iOS and web browser, you can’t use Application.CaptureScreenshot–you’ll need to use the GetPixels function. But if you’re just building a game for the desktop, Application.CaptureScreenshot will work fine. Also, It sounds like you just want to use a screenshot as a Texture2D, which you can do either on the GUI or on a plane in front of the camera. Which one you want to do is up to you–but for iOS or for some picture that’s going to be changing every frame, I wouldn’t use the GUI. But basically, if you want it to save the image to a “secure” location on a computer, you can use this script–although this script will erase over the old screenshot every time. If you want to save multiple screenshots, you’ll have to write code that generates a new filename every time you push “P”.
Here’s a function that does what you want, I think:
function turnScreenshotsIntoTexture(imageName : String) : Texture2D
{
var tex : Texture2D;
var fileName = "/" + imageName + ".png";
filePath = "file:///" + Application.persistentDataPath + fileName;
var www : WWW = new WWW (filePath);
yield www;
return www.texture;
}
}
Here’s another script, that uses the “ReadPixels” method and allows for multiple images. I’ve messed with it a bit for you, but it should work alright:
private var imageName = "characterImage";
private var imageCount = 0;
function Update()
{
if(Input.GetKeyDown(KeyCode.P))
{
renderer.enabled = true;
// YOU CAN CALL takeScreenShot() HERE AND IT WILL RETURN THE TEXTURE2D TO WHATEVER YOU WANT
}
}
function takeScreenShot() : Texture2D
{
var tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
var myRect = Rect(0, 0, Screen.width, Screen.height);
tex.ReadPixels (myRect, 0, 0, false);
tex.Apply(false, false);
if(!Application.isWebPlayer)
{
var fileName = "/" + imageName + imageCount.ToString() + ".png";
Application.CaptureScreenshot(Application.persistentDataPath + fileName);
filePath = "file:///" + Application.persistentDataPath + fileName;
}
imageCount++;
return tex;
}
Working script:
var filePath;
var imageName = "pictureOfBradPitt";
function Start()
{
filePath = "file:///" + Application.dataPath + "/" + imageName + ".png";
print(filePath);
}
// Take a shot immediately
function Update ()
{
if(Input.GetKeyDown(KeyCode.P))
{
Application.CaptureScreenshot(Application.dataPath + "/" + imageName + ".png");
turnScreenShotIntoTexture();
}
}
function turnScreenShotIntoTexture()
{
yield WaitForEndOfFrame;
var www : WWW = new WWW (filePath);
yield www;
myTexture =
renderer.material.mainTexture = www.texture;
}
system
3
Hi sgbraunstein,i am currently doing somethings similar to your script,but i am new and no idea how to do it can you help me
I wanted to do a script that When i press Key(P).it will cature the screenshot and i wanted the saved screenshot to be display as a smaller view beside my screen, can you help me or teach me Thx
system
5
Hi sgbraunstein,i think you abit misunderstand what i saying,actually i have this script
using UnityEngine;
using System.Collections;
public class TakeScreenshot : MonoBehaviour
{
private int screenshotCount = 0;
// Check for screenshot key each frame
void Update()
{
// take screenshot on up->down transition of F9 key
if (Input.GetKeyDown("k"))
{
string screenshotFilename;
do
{
screenshotCount++;
screenshotFilename = "screenshot" + screenshotCount + ".png";
} while (System.IO.File.Exists(screenshotFilename));
Application.CaptureScreenshot(screenshotFilename);
}
}
}
This script works the same as cature a screenshot and save it in the file by pressing k
what i mean is i wanted to add in another effects and thats is when i cature the screenshot,i not only want to save this screenshot but in the same time also display the screenshot on the scene at a smaller view>>just like when we press on camera a smaller view pop beside<<
So i create a GUITexture and put the Texture to None(Texture),
so i wanted it to be like this
When i press k,it will screenshot and put this screenshot into the GUITexture
system
6
Hi sgbraunstein,pls help me why your script is not working??its not caturing any view or what??
Sorry, was working on it last night, but didn’t finish. Will finish tonight, I think! Give me 7 hours or so!