Unity screenshot full screen

The Application.CaptureScreenshot method in U3d is a method to take a full screen and save it in a
picture
Note: The picture is saved one frame or more after the Application.CaptureScreenshot call is completed, so you have to judge that the picture has been saved after the Application.CaptureScreenshot call is completed before you can use it. . Otherwise, you will find that you cannot find the pictures.

2. It saves the path problem. You can pass it a full path under Application.persistentDataPath
on the pc,
but in android and ios, you can only pass it a related path under Application.persistentDataPath.

3. Implementation method example

/// <summary>
/// 简单截屏的方法
/// </summary>
/// <param name="path"></param>
/// <param name="callback"></param>
/// <returns></returns>
public static IEnumerator CaptureScreenshot(string path, System.Action<bool, string> callback)
{
    // 路径处理
    // 注:path是一个在Application.persistentDataPath全路径
    // Application.persistentDataPath这个方法的参数:
    //      在pc呆传全路径
    //      在android和ios上可能传Application.persistentDataPath下的相关路径

    string newPath = path;
#if !UNITY_EDITOR
    newPath = path.Replace(Application.persistentDataPath + "/", "");
#endif
    //Debugger.Log("CaptureScreenshot path: " + path);
    //Debugger.Log("CaptureScreenshot newPath: " + newPath);
    Application.CaptureScreenshot(newPath);
    float time = Time.time;
    bool b = false;
    yield return new WaitUntil(() =>
    {
        b = System.IO.File.Exists(path);
        return b || ((Time.time - time) > 1f);
    });
    string str = path;
    if (b == false)
    {
        str = "截屏出错!";
    }
    if (callback != null)
    {
        callback(b, str);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483384&siteId=291194637