4

我正在尝试保存我的 Unity 项目的框架以自动生成预览 Gif。我在我的脚本中调用该方法,如下所示:

Application.CaptureScreenshot("Screenshot" + Time.frameCount + ".png");

如果我在 Unity 中这样做,它工作得很好。但是当我将我的项目构建为 WebGL 时,它不再这样做了。为什么?或者有人知道另一种保存框架的方法吗?

4

3 回答 3

1

使用ScreenCapture.CaptureScreenshotAsTexture函数:

void CamptureScreenShot(){
    yield return new WaitForEndOfFrame();
    Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture(1);
    byte[] bytes = texture.EncodeToPNG();
}

然后你可以对图像做一些事情:例如将它保存到服务器

void SaveFunction(byte[]  bytes){
    string imageName = username + "_" + date + ".png";
    WWWForm form = new WWWForm();
    form.AddField("frameCount", Time.frameCount.ToString());
    form.AddBinaryData("file", bytes, imageName, "image/png");

    using (UnityWebRequest request = UnityWebRequest.Post("my url service", form))
    {

        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
        }
        else
        {
            Debug.Log("Saved");
        }
    }
}
于 2019-08-15T15:45:02.603 回答
0
//    Application.CaptureScreenshot is not for WebGL..

//    WebGL must use read pixel for capture screen.

 //   Guess you trying to export some files from WebGL, then should use .php or //something else. Beacause WebGL requires stp connection.


    IEnumerator SendImagetophp()
    {
            //Path to PHP
            string screenShotURL = "http://youtphppath/yourphpname.php";


            //Wait until frame ends
            yield return new WaitForEndOfFrame();

            // Create a texture the size of the screen, RGB24 format
            int width = Screen.width;
            int height = Screen.height;
            tex = new Texture2D(width, height, TextureFormat.RGB24, false);

            // Read screen contents into the texture        
            tex.ReadPixels(new Rect(0,0, width, height), 0, 0, false);
            tex.Apply();


            // Encode texture into PNG
            byte[] bytes = tex.EncodeToPNG();
            Destroy(tex);

            // Create a Web Form
            WWWForm form = new WWWForm();
            form.AddField("frameCount", Time.frameCount.ToString());
            //form.AddField("email", userEmailAddress);
            form.AddBinaryData("file", bytes, "_screenShot.png", "multipart/form-data");//"image/png"

            // Post WWWForm to path
            UnityWebRequest www = UnityWebRequest.Post(screenShotURL, form);

            yield return www.SendWebRequest();
            //Debug
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Image Uploaded!");
            }
    }

//.PHP file _ require upload folder.
 <?php
$file_name = $_FILES['file']['name'];
$tmp_file = $_FILES['file']['tmp_name'];
$file_path = 'upload/'.$file_name;

$r = move_uploaded_file($tmp_file, $file_path);
?>
于 2019-09-10T06:55:40.007 回答
0

还有其他方法可以截取屏幕截图。未在 WebGL 上测试,但您应该尝试一下。

void CaptureScreenshot()
{
    StartCoroutine(CaptureScreenshotCRT());
}

IEnumerator CaptureScreenshotCRT()
{
    yield return new WaitForEndOfFrame();
    string path = Application.persistentDataPath + "/Screenshot" + Time.frameCount + ".png";
    Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
    //Get Image from screen
    screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    screenImage.Apply();
    //Convert to png
    byte[] imageBytes = screenImage.EncodeToPNG();

    //Save image to file
    System.IO.File.WriteAllBytes(path, imageBytes);
}
于 2016-06-16T11:58:26.853 回答