Unity下截屏并直接保存到相册的插件!


直接调用:ScreenshotHelper.iCaptureScreen(CaptureScreenCallBack);即可实现截屏。 函数void CaptureScreenCallBack(Texture2D texture2D) { new FilePathName().SaveTextureAs(texture2D,FilePathName.SaveFormat.JPG); } 是截屏成功后的回调函数,负责保存图片。 简单易用!经测试,无问题!
资源截图
代码片段和文件信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

public class FilePathName
{
public enum SaveFormat
{
GIF = 0
JPG
PNG
}

public string GetSaveDirectory()
{
//GIF store in Virtual Memory
//Available path: Application.persistentDataPath Application.temporaryCachePath Application.dataPath
//Do not allow sub-Folder under the path. If you need to view gif you can filter the file names to include .gif only.
#if UNITY_EDITOR
return Application.dataPath; 
#else
// return Application.persistentDataPath;
return “/storage/emulated/0/DCIM/“;
#endif
}

public string GeFileNameWithoutExt()
{
return DateTime.Now.ToString(“yyyy-MM-dd-HH-mm-ss“);
}

public string GetGifFileName()
{
string timestamp = GeFileNameWithoutExt();
return “GIF_“ + timestamp;
}
public string GetGifFullPath()
{
return GetSaveDirectory() + “/“ + GetGifFileName() + “.gif“;
}
public string GetDownloadedGifSaveFullPath()
{
return GetSaveDirectory() + “/“ + GetGifFileName() + “.gif“;
}

public string GetJpgFileName()
{
string timestamp = GeFileNameWithoutExt();
return “JPG_“ + timestamp;
}
public string GetJpgFullPath()
{
return GetSaveDirectory() + “/“ + GetJpgFileName() + “.jpg“;
}

public string GetPngFileName()
{
string timestamp = GeFileNameWithoutExt();
return “PNG_“ + timestamp;
}
public string GetPngFullPath()
{
return GetSaveDirectory() + “/“ + GetPngFileName() + “.png“;
}

public byte[] ReadFileToBytes(string fullPath)
{
return File.ReadAllBytes(fullPath);
}

public void WriteBytesToFile(string toFullpath byte[] byteArray)
{
File.WriteAllBytes(toFullpath byteArray);
}

public void FileStreamTo(string fullpath byte[] byteArray)
{
using( FileStream fs = new FileStream(fullpath FileMode.Create FileAccess.Write) )
{
fs.Write(byteArray 0 byteArray.Length);
}
}

public string SaveTextureAs(Texture2D texture2D SaveFormat format = SaveFormat.JPG)
{
string savePath = string.Empty;
switch(format)
{
case SaveFormat.JPG:
savePath = GetJpgFullPath ();
Debug.Log (“savePath is: “+savePath);
WriteBytesToFile(savePath texture2D.EncodeToJPG(90));
break;
case SaveFormat.PNG:
savePath = GetPngFullPath();
WriteBytesToFile(savePath texture2D.EncodeToPNG());
break;
case SaveFormat.GIF:
//savePath = ProGifTexture2DsToGIF.Instance.Save(new List{texture2D} texture2D.width texture2D.height 1 0 10);
break;
}
return savePath;
}

// public string SaveTexturesAsGIF(List textureList int width int height int fps int loop int quality
// Action onFileSaved = null Action onFileSaveProgress = null 
// ProGifTexture2DsToGIF.ResolutionHandle resolutionHandle = ProGifTexture2DsToGIF.ResolutionHandle.ResizeKeepRatio)
// {
// return ProGifTexture2DsToGIF.Instance.Save(textureList width height fps loop quality onFileSaved onFileSaveProgress

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     117072  2017-10-28 22:37  SWAN Dev3rd PartyFontsDroidSansMono.ttf

    ..A..H.       344  2017-10-28 22:37  SWAN Dev3rd PartyFontsDroidSansMono.ttf.meta

    ..A..H.       193  2017-10-28 22:37  SWAN Dev3rd PartyFonts.meta

    ..A..H.       193  2017-10-28 22:37  SWAN Dev3rd Party.meta

     文件       3031  2018-02-09 17:16  SWAN DevCommonFilePathName.cs

    ..A..H.       264  2017-10-28 22:37  SWAN DevCommonFilePathName.cs.meta

     文件       3392  2017-10-28 22:37  SWAN DevCommonSDemoAnimation.cs

    ..A..H.       264  2017-10-28 22:37  SWAN DevCommonSDemoAnimation.cs.meta

     文件       1025  2017-10-28 22:37  SWAN DevCommonSelfAnimation.cs

    ..A..H.       264  2017-10-28 22:37  SWAN DevCommonSelfAnimation.cs.meta

    ..A..H.       193  2017-10-28 22:37  SWAN DevCommon.meta

     文件     240484  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto01.jpg

    ..A..H.      2294  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto01.jpg.meta

     文件     453943  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto02.jpg

    ..A..H.      2294  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto02.jpg.meta

     文件     159981  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto03.jpg

    ..A..H.      2294  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto03.jpg.meta

     文件     358097  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto04.jpg

    ..A..H.      2294  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto04.jpg.meta

     文件     540128  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto05.jpg

    ..A..H.      2294  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto05.jpg.meta

    ..A..H.       193  2017-10-28 22:37  SWAN DevScreenshotHelperImagesPhoto.meta

    ..A..H.       193  2017-10-28 22:37  SWAN DevScreenshotHelperImages.meta

     文件       2454  2018-02-09 16:51  SWAN DevScreenshotHelperMaterialsSpriteDefault.mat

    ..A..H.       181  2017-10-28 22:37  SWAN DevScreenshotHelperMaterialsSpriteDefault.mat.meta

     文件       2454  2018-02-09 16:51  SWAN DevScreenshotHelperMaterialsSpriteDiffuse.mat

    ..A..H.       181  2017-10-28 22:37  SWAN DevScreenshotHelperMaterialsSpriteDiffuse.mat.meta

    ..A..H.       193  2017-10-28 22:37  SWAN DevScreenshotHelperMaterials.meta

     文件      25437  2017-10-28 22:37  SWAN DevScreenshotHelperReadme - Screenshot Helper 1.02.pdf

    ..A..H.       176  2017-10-28 22:37  SWAN DevScreenshotHelperReadme - Screenshot Helper 1.02.pdf.meta

............此处省略26个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)