- Home /
Question by
w4der · Aug 20, 2012 at 11:55 AM ·
androidiosscreenshot
Screenshot on iOS and Android not working
Hi there, I have problem with taking screenshot on iOS and Android. Using Unity 3D editor all working fine. On Android it make something like that:
On iOS it shows me pictures with question marks. Please help me. Can someone do this for me? P.S. Sorry for my english ;) Code that causes this issue is below. I'm using Win 7 64 bit and Unity 3D 3.5.5f
using UnityEngine;
using System.Collections;
using System.IO;
public class PaintScreenshot : MonoBehaviour
{
public string fileName; // Name of the screenshot - without '.png'
public string fileNameThumb; // Name of the thumbnail screenshot - without '.png'
string path;
WWW tex;
public static PaintScreenshot instance
{
get{
if( m_Instance == null )
{
GameObject go = new GameObject("Screenshot Manager");
m_Instance = go.AddComponent<PaintScreenshot>() as PaintScreenshot;
}
return m_Instance;
}
}
private static PaintScreenshot m_Instance;
public void TakeScreenshotStart()
{
StartCoroutine( TakeScreenshot() );
}
IEnumerator TakeScreenshot()
{
// yield return StartCoroutine( UploadPNG(0.05f) ); // Start Old Version of TakeScreenshot and comment lines below this line
yield return StartCoroutine( TakeScreenshot(0.05f) );
fileName = System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff"); // File name based on date and time
fileNameThumb = fileName + "-thumb";
fileName += ".png";
fileNameThumb += ".png";
path = Application.persistentDataPath;
Application.CaptureScreenshot(path + "/" + fileName);
// Save thumb
// More help at: http://answers.unity3d.com/questions/18647/unity-iphone-can-i-store-data-on-the-iphone-to-be.html
Texture2D tex = new Texture2D( 150, 150, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect((Screen.width - 150) / 2, (Screen.height - 150) / 2, 150, 150), 0, 0 ); // Thumb Screenshot only from center of the screen
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
FileStream fs = new FileStream(path + "/" + fileNameThumb, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(bytes);
w.Close();
fs.Close();
Debug.Log("Screenshot at: " + path + "/" + fileName);
yield return new WaitForSeconds(0.05f);
print("Screenshot is ready");
}
IEnumerator TakeScreenshot( float seconds )
{
yield return new WaitForSeconds(seconds);
}
byte[] GetBytes()
{
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
return bytes;
}
byte[] GetBytesThumb()
{
// Create a texture only from part of the screen, RGB24 format
// int width = thumbWidth;
// int height = thumbHeight;
Texture2D tex = new Texture2D( 150, 150, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect((Screen.width - 150) / 2, (Screen.height - 150) / 2, 150, 150), 0, 0 ); // Thumb Screenshot only from center of the screen
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
return bytes;
}
IEnumerator UploadPNG( float seconds )
{
yield return new WaitForSeconds(seconds);
// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame();
byte[] bytes = GetBytes();
fileName = System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff"); // File name based on date and time
fileNameThumb = fileName + "-thumb";
Debug.Log("Normal: " + Application.persistentDataPath + "/" + fileName + ".png");
Debug.Log("Thumb: " + Application.persistentDataPath + "/" + fileNameThumb + ".png");
FileStream fs = new FileStream(Application.persistentDataPath + "/" + fileName + ".png", FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(bytes);
w.Close();
fs.Close();
byte[] bytesThumb = GetBytesThumb();
FileStream fsThumb = new FileStream(Application.persistentDataPath + "/" + fileNameThumb + ".png", FileMode.Create);
BinaryWriter wThumb = new BinaryWriter(fsThumb);
wThumb.Write(bytesThumb);
wThumb.Close();
fsThumb.Close();
yield return new WaitForSeconds(seconds);
print("screenshot is ready");
}
}
thumbs.jpg
(120.6 kB)
Comment
Answer by Paulius-Liekis · Aug 20, 2012 at 12:08 PM
Try adding coroutine WaitForEndOfFrame call before Application.CaptureScreenshot.