- Home /
Taking and storing a screenshot locally to later load on UI canvas (all in WebGL)
Unity WebGL clients are unable to save files on a user's computer because it runs in a browser, but I still want to be able to store files, even if they are only stored in some kind of virtual environment (as in, not a file physically stored permanently in the user's HDD folder). I only need files to be accessible by pointing to a path, even if its virtual, in order to be able to use Braincloud's Upload File API that requires a path declaration like this: string localPath = "path/to/my/file.dat"; (Braincloud is an SDK provider) So I've made a screenshot script that stores a file in Application.persistentDataPath and a Load texture script but unfortunately, I'm unable to make it work.
Would love to get some help.
TakeScreenshot Script:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
public class TakeScreenshot : MonoBehaviour {
// Using this for initialization
public void Screenshot () {
StartCoroutine(UploadPNG());
}
IEnumerator UploadPNG() {
// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
string file = Application.persistentDataPath+"/test.png";
var fileData = tex.EncodeToPNG();
File.WriteAllBytes(file, fileData);
StreamReader reader = new StreamReader(file);
Debug.Log(reader.ReadToEnd());
reader.Close();
}
}
LoadTexture Script:
using UnityEngine;
using UnityEngine.UI;
public class LoadTexture : MonoBehaviour {
Texture2D myTexture;
// Using this for initialization
public void LoadScreenshot() {
// load texture from resource folder
myTexture = Resources.Load (Application.persistentDataPath+"/test.png") as Texture2D;
GameObject rawImage = GameObject.Find ("RawImage");
rawImage.GetComponent<RawImage> ().texture = myTexture;
Debug.Log ("Applied");
}
}
Answer by highpockets · Aug 23, 2020 at 09:52 PM
I’m not sure about that asset you mention, but you might want to try PlayerPrefs in this case. It will be saved in the browser indexedDB:
byte[] texAsByte = texture.EncodeToPNG();
string texAsString = Convert.ToBase64String(texAsByte);
string myKey = "MyTex";
PlayerPrefs.SetString(myKey, texAsString);
From my understanding, the file size with PlayerPerfs is limited to 1$$anonymous$$B. Still, I'll give it a shot and share my findings.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Persistent data between different games - local save for WebGL 1 Answer
How to Write streamingAssets folder files to webgl persistent data at run time? 0 Answers
Why do PlayerPrefs become empty when uploading a new WebGL game version to Facebook 0 Answers
Save file is not detected, persistentdatapath changed? 2 Answers
Application.CaptureScreenshot() does not save any image on Android while it does on Editor! 0 Answers