I’m using Unity 5.6.4 and I’ve got a game where you’ve got to find a bunch of stuff and when you find them all you go to a scene where there’s confetti raining down and it says “you took xx:xx:xx” I want a button at the bottom where you click and it save a screenshot of the time taken. the game will be on a site and as an .exe. I’ve got the screenshot for the .exe version done and working its the downloading from webGL that isn’t working. I know I need a .jslib to have the download function and then call that from the unity script.
In Assets/Plugins I’ve got download.jslib
mergeInto(LibraryManager.library, {
Download: function (data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
});
This is called in the unity file save_Screenshot.cs
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class Save_Screenshot : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Download(byte[] data, string filename, string type);
private static Save_Screenshot instance;
private Camera myCamera;
private bool takeScreenshot = false;
private void Awake () {
instance = this;
myCamera = gameObject.GetComponent<Camera> ();
}
private void TakeScreenshot(int width, int height){
myCamera.targetTexture = RenderTexture.GetTemporary (width, height, 16);
takeScreenshot = true;
}
IEnumerator OnPostRender() {
if (takeScreenshot) {
yield return new WaitForEndOfFrame ();
takeScreenshot = false;
RenderTexture renderTexture = myCamera.targetTexture;
Texture2D renderResult = new Texture2D (renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
Rect rect = new Rect (0, 0, renderTexture.width, renderTexture.height);
renderResult.ReadPixels (rect, 0, 0);
byte[] byteArray = renderResult.EncodeToPNG ();
Download (byteArray, "Time Taken.png", "image/png");
RenderTexture.ReleaseTemporary (renderTexture);
myCamera.targetTexture = null;
}
}
public static void staticScreenshot(int width, int height){
instance.TakeScreenshot (width, height);
}
}
When i run this in editor to test it I get an error
EntryPointNotFoundException: Download
Save_Screenshot+<OnPostRender>c__Iterator0.MoveNext () (at Assets/Scripts/Save_Screenshot.cs:38)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Thinking this may be because I’m trying to do it in editor not on a browser I tried to build it but I got a error saying its failed its sanity check
LSupport\\BuildTools\\emscripten.config_sanity'
stderr:WARNING:root:did not see a source tree above or next to the LLVM root directory (guessing based on directory of C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten_FastComp_Win\llc), could not verify version numbers match
INFO:root:(Emscripten: Running sanity checks)
WARNING:root:java does not seem to exist, required for closure compiler, which is optional (define JAVA in C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\emscripten.config if you want it)
WARNING:root:closure compiler will not be available
WARNING:root:--separate-asm works best when compiling to HTML. otherwise, you must yourself load the '.asm.js' file that is emitted separately, and must do so before loading the main '.js` file
error: failure to execute js library "L:\unity\Test\Assets\Plugins\test.jslib": SyntaxError: Unexpected token ILLEGAL,,SyntaxError: Unexpected token ILLEGAL at Object.LibraryManager.load (eval at globalEval (C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:173:14) at JSify (eval at globalEval (C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:59:20) at L:\unity\Test\Assets\Plugins\test.jslib (C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:208:3) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3preprocessed source (you can run a js engine on this to get a clearer error message sometimes):=============��m
UnityEditor.HostView:OnGUI()
And another error
L/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:437)
UnityEditor.WebGL.WebGlBuildPostprocessor.PostProcess (BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:877)
UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTargetGroup targetGroup, BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.BuildReporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:186)
UnityEditor.HostView:OnGUI()
Any idea what I’m doing wrong?