【问题标题】:Why taking screenshot with System.IO doesn't work?为什么使用 System.IO 截屏不起作用?
【发布时间】:2020-09-09 19:13:29
【问题描述】:

我已经添加到我的游戏截图功能。在 Xcode 中构建它,但出现““FileNotFoundException:找不到文件”错误。为什么?

private IEnumerator TakeScreenshot()
{

    string imageName = "LevelFinished" + current_level.ToString() + ".png";

    ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/" + imageName);
    yield return new WaitForSeconds(0.3f);

    byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/" + imageName);
    Texture2D screenshotTexture = new Texture2D(Screen.width, Screen.height);
    screenshotTexture.LoadImage(data);
    Sprite screenshotSprite = Sprite.Create(screenshotTexture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f));

    UI_Image_final.transform.GetChild(0).GetComponent<Image>().sprite = screenshotSprite;

}

【问题讨论】:

    标签: c# ios unity3d


    【解决方案1】:

    来自docs

    包含持久数据目录的路径(只读)。

    如果这是正确的,则意味着您的屏幕截图无法存储在那里,因此如果您检查您的 persistentDataPath 目录,您将找不到图片。尝试删除加载纹理的部分代码,然后尝试存储它,然后检查文件是否存在。

    存储此类内容的一种方法是使用文件夹StreamingAssets 来存储屏幕截图。我知道,文档说:

    在许多平台上,流资产文件夹位置是只读的

    但是我已经做了一个测试,至少对于 Windows 和 Android,它可以让你从那个文件夹中读写。

    【讨论】:

      【解决方案2】:

      解决方案很简单:在 ScreenCapture.CaptureScreenshot 的 docs 中,我错过了“在移动平台上,文件名附加到持久数据路径”部分。这意味着,我保存屏幕截图的路径不正确;因此我只需要更改一行:

      //ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/" + imageName);
      //Change for:
      ScreenCapture.CaptureScreenshot(imageName);
      

      【讨论】:

        猜你喜欢
        • 2011-10-10
        • 2022-08-20
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-16
        相关资源
        最近更新 更多