我是新手学习Unity,想要保存一张图片并在之后使用。我尝试用Application.CaptureScreenshot和Texture2D.ReadPixel的方法,并尝试将它们保存到persistentDataPath(/data/user/0/my.package.name/files/)、/sdcard/Download/和/storage/emulated/0/Download中,但是没有任何方法有效。在我的项目的每个清单中都有WRITE_EXTERNAL_STORAGE权限。如果我保存到持久数据,我找不到它,我只能找到UnityAds的缓存,如果我保存到下载文件夹,我会收到访问权限被拒绝的错误提示。
可以帮助我吗?
以下是我的代码:
我做错了什么?
可以帮助我吗?
以下是我的代码:
IEnumerator ScreenShot(){
yield return new WaitForEndOfFrame ();
Application.CaptureScreenshot ("ball.png");
Application.CaptureScreenshot ("/sdcard/Download/ball.png");
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
tex.Apply ();
byte[] bytes = tex.EncodeToJPG ();
File.WriteAllBytes ("sdcard/Download/ball.png", bytes);
File.WriteAllBytes (Application.persistentDataPath + "/ball.png", bytes);
}
在检查文件夹是否存在之前:
if(!System.IO.Directory.Exists("/sdcard/Download/"))
{
System.IO.Directory.CreateDirectory ("/sdcard/Download/");
}
if(!System.IO.Directory.Exists(Application.persistentDataPath))
{
System.IO.Directory.CreateDirectory (Application.persistentDataPath);
}
我使用以下代码开始IEnumerator:
StartCoroutine(ScreenShot());
我做错了什么?