在Unity WebGl中保存帧

4

我正在尝试保存Unity项目的帧以自动生成预览GIF。我可以通过以下方式在脚本中调用该方法:

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

如果我在Unity中这样做,它可以正常工作。但是,当我将我的项目构建为WebGL时,它不再起作用了。为什么?或者有没有人知道另一种保存帧的方法?

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");
        }
    }
}

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);
}

这个解决方案在我的应用程序中根本不保存任何屏幕截图。 - Ipomea
它现在在Unity中可以工作,但仍然无法作为webGL导出 :/ - Ipomea
@MaryLu 首先,你说它甚至没有保存任何屏幕截图,却没有提供更多信息,现在你又说它在Unity中可以工作,但在WebGL中不行。我忽略这样的评论。 - Programmer
我不知道为什么之前它没能工作。我只是复制了你的代码。这一次我在一个新项目中再次尝试了一下,它就可以工作了。我不知道为什么,因为从我所看到的来看,我做的完全一样。这就是为什么我不能提供更多信息的原因。对此感到抱歉。 - Ipomea

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);
?>

1
请在您的帖子中包含解释如何以及为什么解决问题,这将有助于提高您的帖子质量,并可能导致更多的赞同。 - Android

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接