Unity自带的截屏Android上保存不了

不知道原因的,使用CaptureScreenshot不能保存到截图到安卓的文件系统中

所以使用另一种方式,成功了。

public IEnumerator CaptureScreenShow()
{
    yield return new WaitForEndOfFrame();
    string fileName = DateTime.Now.Ticks.ToString();
    string tagerFolder = DateTime.Now.ToString("yyyy-MM-dd");
    string pathName = Path.Combine(Application.persistentDataPath, tagerFolder);
    if (!Directory.Exists(pathName))
    {
        Directory.CreateDirectory(pathName);
    }

    string path = pathName+"/"+fileName + ".png";
    //Application.CaptureScreenshot(path);
    
    //截全屏,当然你可以控制大小
    Texture2D t = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
    t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
    t.Apply();

    byte[] byt = t.EncodeToPNG();//转换为字节
    File.WriteAllBytes(path, byt);//保存到路径的文件中

    yield return null;

    //判断是否截图成功
    if (File.Exists(path))
    {
        Debug.Log("截图成功!");
    }
    else {
        Debug.Log("截图失败!");
    }

        
}

其实应该就是读取屏幕的像素保存到Texture中,然后将Texture保存到文件系统中。

读取屏幕的像素,应该是渲染完成后再读取,所以要先等待到帧结束时再读取保存。


首页 我的博客
粤ICP备17103704号