在UntyWebGl中保存帧

时间:2016-06-16 10:28:57

标签: c# unity3d unity-webgl

我正在尝试将Unity项目的帧保存为自动生成预览Gif。 我在我的脚本中调用这个方法:

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

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

3 个答案:

答案 0 :(得分: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");
        }
    }
}

答案 1 :(得分: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);
}

答案 2 :(得分: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);
?>