- Home /
Capture Screenshot not working in WebGL
I tried making a script that uses the main camera to take a screenshot of the object. It works well in unity without any errors but I tried to build it as a WebGL. Sadly, it shows an error that tells me to enable the exception to know it. Enabled it and didn't figure out anything at all. Is there something I missed with the WebGL coding? Any help would be appreciated. Thank you!
using UnityEngine;
using System.Collections;
public class ScreenCapture : MonoBehaviour {
public int resWidth = 960;
public int resHeight = 600;
public static string ScreenShotName(int width, int height) {
return string.Format("C:/PerfLogs/screen_{1}x{2}_{3}.png",
Application.dataPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
public void ScreenshotButton() {
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
GetComponent<Camera>().targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
GetComponent<Camera>().Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
GetComponent<Camera>().targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
Debug.Log("Screenshot Saved!");
}
}
Here's a picture of the error.
Answer by sinha131 · Feb 20, 2018 at 11:01 AM
To go > Edit > Project Settings > Players
And then verify whether Enable Exceptions is set as given in picture
Your answer
Follow this Question
Related Questions
[WEBGL] PLAYER CONTINUES TO RUN AFTER WEBGL-CONTENT IS REMOVED FROM DOM 1 Answer
WebGL game doesn't run: Uncaught abort(215) 1 Answer
i cant use wepGL in unity 0 Answers
WebGL loader stuck at 90% 0 Answers