UNITY C#截图问题

时间:2016-08-28 07:52:59

标签: c# unity3d

我试图编写一些代码来截取并将其保存在Android默认的DCIM路径中,第一部分用于截取屏幕截图,但第二部分用于移动文件到路径不起作用。

public class IS_Screenshot : MonoBehaviour
{
    string ScreenShotFile;

    void Start () 
    {
        ScreenShotFile = Application.persistentDataPath + "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss") + ".png";
        Debug.Log (ScreenShotFile);
    }

    public void Screen_Shot() 
    {
        try
        {
            Application.CaptureScreenshot(ScreenShotFile);

            string Path = "/mnt/sdcard/DCIM/" + "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ".png";
            Debug.Log (Path);

            if(System.IO.File.Exists(ScreenShotFile))
            {
                System.IO.File.Move(ScreenShotFile, Path);
                Debug.Log ("Screenshot file saved.");
            }
            else
            {               
                Debug.Log ("Screenshot file not found.");
            }
        }
        catch(Exception ex) 
        {
            Debug.Log ("Screenshot capture failed. | " + ex);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用Path.Combine将文件名附加到persistentDataPath。很可能在Android上,persistentDataPath的值不会在路径分隔符中结束,这会导致路径无效。

ScreenShotFile = Path.Combine(Application.persistentDataPath, "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss") + ".png");