版权声明:欢迎转载,转载请注明原博客链接:http://blog.csdn.net/u013108312 和本文章的链接。谢谢合作。作者:CircleGood https://blog.csdn.net/u013108312/article/details/78002722
private static string _TextureUrl;
public static string TextureUrl
{
set { _TextureUrl = value; }
get
{
if (_TextureUrl == null)
{
// "/sdcard/Pictures/Test";
#if UNITY_ANDROID && !UNITY_EDITOR
string temp =Application.persistentDataPath;
DirectoryInfo diInfo = new DirectoryInfo(temp);
_TextureUrl=diInfo.Parent.Parent.Parent.Parent.FullName+"/Pictures/Test";
#elif UNITY_IPHONE && !UNITY_EDITOR
_TextureUrl = Application.persistentDataPath+"/Test";
#else
_TextureUrl = Application.dataPath + "/Test";
#endif
System.IO.Directory.CreateDirectory(_TextureUrl);
}
return _TextureUrl;
}
}
public static string GetFileNameToTime
{
get
{
return "tb_" + DateTime.Now.ToFileTime().ToString() + ".jpg";
}
}
public void ScreenShotStart (string parem)
{
Debug.Log ("This is param: " + parem);
string fileName = "";
if (string.IsNullOrEmpty (parem))
fileName = AppConfig.TextureUrl + "/" + AppConfig.GetFileNameToTime;
else
fileName = AppConfig.TextureUrl + "/" + parem;
StartCoroutine (CaptureScreenshot (MainCamera, fileName));
}
/// <summary>
/// 截图操作
/// </summary>
/// <returns>The screenshot.</returns>
/// <param name="mCamera">相机</param>
/// <param name="mFileName">完整路径</param>
IEnumerator CaptureScreenshot (Camera mCamera, string mFileName)
{
Rect rect = new Rect ();
rect.width = Screen.width;
rect.height = Screen.height;
//等待渲染线程结束
yield return new WaitForEndOfFrame ();
//初始化RenderTexture
RenderTexture mRender = new RenderTexture ((int)rect.width, (int)rect.height, 0);
//设置相机的渲染目标
mCamera.targetTexture = mRender;
//开始渲染
mCamera.Render ();
//激活渲染贴图读取信息
RenderTexture.active = mRender;
Texture2D mTexture = new Texture2D ((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
//读取屏幕像素信息并存储为纹理数据
mTexture.ReadPixels (rect, 0, 0);
//应用
mTexture.Apply ();
//释放相机,销毁渲染贴图
mCamera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy (mRender);
//将图片信息编码为字节信息
byte[] bytes = mTexture.EncodeToPNG ();
//保存
File.WriteAllBytes (mFileName, bytes);
yield return bytes;
#if UNITY_IPHONE && !UNITY_EDITOR
CallIosMeth.ShowImgToAlbum(mFileName);
#endif
}
/// <summary>
/// IOS 保存相册操作
/// </summary>
/// <param name="typeid">Typeid.</param>
public static void ShowImgToAlbum (string path)
{
#if UNITY_IPHONE
ImgToAlbum(path);
#endif
}
#if UNITY_IPHONE
[DllImport ("__Internal")]
private static extern void ImgToAlbum (string path);
#endif