要做一个截图的功能,并且玩家可以在相册中看到。
做的时候遇到了三个问题:
1、unity自带的截图API,Application.CaptureScreenshot在Android上不生效
2、图片保存的路径获取
3、保存的图片可以在手机的文件管理中找到,但是相册中没有。
解决方案:
1、这个问题查了半天没有说原因,大多数人都给出了新方案用 File.WriteAllBytes去实现,代码如下:
IEnumerator CutImage(stringname)
{//图片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);yield return newWaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
tex.Apply();yield returntex;byte[] byt =tex.EncodeToPNG();string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
File.WriteAllBytes(path+ "/Pictures/Screenshots/" +name, byt);}
2、关于路径Application中有四种路径(可自行百度),我们需要的是保存到截图专用的路径Pictures中,代码参考上面最后两行