• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bigzer · Aug 23, 2020 at 03:12 PM · webglscreenshotpersistentdatapath

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






Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bigzer · Aug 30, 2020 at 02:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

What is the capacity of Application.persistentDataPath on WebGL? 1 Answer

Application.CaptureScreenshot() does not save any image on Android while it does on Editor! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges