i need to be able to take a screen capture from within the app and save it to the photo library
i tried very simply adding the
Application.CaptureScreenshot(“Screenshot.png”);
to a button but didnt seem to do anything at all
any help?
i need to be able to take a screen capture from within the app and save it to the photo library
i tried very simply adding the
Application.CaptureScreenshot(“Screenshot.png”);
to a button but didnt seem to do anything at all
any help?
im trying to use the following script
// Saves screenshot as PNG file.
import System.IO;
private var gui :GUITexture;
function Start(){
gui=GetComponent(GUITexture);
}
function Update(){
var count = Input.touchCount;
for (var i: int =0; i<count; i++){
var touch :Touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began gui.HitTest(touch.position))
{
////////////////////
UploadPNG ();
//////////////////
//change color on tap
guiTexture.color = Color(.1,.3,.4);
}
if (touch.phase == TouchPhase.Ended)
{
guiTexture.color = Color.grey;
}
}
}
// Take a shot immediately
UploadPNG ();
function UploadPNG () {
// We should only read the screen bufferafter rendering is complete
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
tex.Apply ();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy (tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "eppx2010.app/Data", bytes);
}
this works when i test with the remote and put a desktop application data path, i cant seem to find where to save it when its on the ipad tho… any ideas?
@pat_sommer, the only writeable directory on iOS devices is the Documents directory.
ive been finding that out a little,
ive changed it to the following
// Saves screenshot as PNG file.
import System.IO;
private var gui :GUITexture;
function Start(){
gui=GetComponent(GUITexture);
}
function Update(){
var count = Input.touchCount;
for (var i: int =0; i<count; i++){
var touch :Touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began gui.HitTest(touch.position))
{
////////////////////
UploadPNG ();
//////////////////
//change color on tap
guiTexture.color = Color(.1,.3,.4);
}
if (touch.phase == TouchPhase.Ended)
{
guiTexture.color = Color.grey;
}
}
}
// Take a shot immediately
UploadPNG ();
function UploadPNG () {
// We should only read the screen bufferafter rendering is complete
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
tex.Apply ();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy (tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../../Documents/Screenshot.png", bytes);
}
still no luck
changed destination to
File.WriteAllBytes(Application.dataPath + “/var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents/Screenshot.png”, bytes);
still no save… gotta be close! are those x’s supposed to be filled?
bump… anyone?
??
bump!
as Prime31 already said you can’t write there. Try persistentDataPath instead
I know it’s a little late but for any other researches that are facing the same problem I think the file directory is
private/var/root/Media/DCIM/100APPLE (or ~/Media/DCIM/100APPLE)
Don’t forget the folder to which such data are actually meant to be written which isn’t documents but the cache directory which you have as well.
Documents is meant for persistent data, as itunes will back it up which in this case isn’t that meaningfull, it only raises the backup time.
the documents folder, also gets backuped to icloud if online backup is selected, for disposable stuff that can be regenerated use caches folder instead, thats what apple recommends
to get into those folders its
DirectoryInfo documentsFolder = new DirectoryInfo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + “/Documents/”;
OR
DirectoryInfo cacheFolder = new DirectoryInfo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + “/Library/Caches/”;
hope this helps
There are many writeable folders. Some relevant ones are:
Application.dataPath + “/…/…” + :
/tmp/ : Good for short term use files. Can be purged without your notice. Not Synced to iTunes or iCloud. Does not persist between updates
/Documents/ : Persists across updates. Can make visible to iTunes (good for images you may want), synced to iCloud/iTunes.
/Library/Caches/ : Persists across updates. Not synced to iTunes/iCloud. Can be purged in rare cases (Low Disk Space/Device Restore)
/Library/Application Support/ : Persists across updates. Syncs with iCloud/iTunes.
Edit: Adding a one more strange but interesting one:
/Documents/Inbox/ : Same rules as Documents, except it can be used by other applications to send data to you; It’s not designed to be written to, but feels like relevant knowledge nonetheless. Really cool because you can also e-mail people attachments with custom app document data.
Without File Sharing enabled Documents and Application Support work very similarly, although Apple may reject your app if you put DLC or App Data into Documents, rather than putting photos, save files, etc… in there.
Application.CaptureScreenshot(filename); works. I only remember an issue in Unity 3.3 were this call would crash if you use MMAA.
it gets written to the documents folder.
This takes some time. Something between 1 and 5 seconds. So you cannot use Application.CaptureScreenshot and immediately use the image afterwards.
i would think you can only write the file to the photolibrary by using objective c. Write a plugin to do so. (Not quite sure about that)
Afaik these two should work on iOS:
Ok so if I’m right what you need to do in order for the iphone application to work is have a GUI Button with the script of the follows :
Excuse me if I’m terribly wrong, I’m learning.
Other then that would I have to create a GUI Button that I script to take to a certain class in Obj-C
where in Xcode I then put the Obj-C terms for capturing a screenshot and save it to the photo library?
Help ![]()
var tex = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
tex.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0, false );
var bytes = tex.EncodeToPNG();