Saving and loading images and audio
Saving and loading images
Saving a Texture2D as an image
You can save a Texture2D as a JPG or a PNG using ES3.SaveImage. The Texture2D must be read/write enabled.
1 2 3 4 5 6 7 |
// Take a screenshot. var texture = new Texture2D(Screen.width, Screen.height); texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); texture.Apply(); // Save the screenshot as a PNG file. ES3.SaveImage(texture, "screenshot.png"); |
Loading an image as a Texture2D
You can load a JPG or PNG as a Texture2D using ES3.LoadImage.
A byte array containing the image data can also be provided as a parameter to this method.
Loading an image from a path
1 2 3 4 5 |
// Load a Texture2D from a PNG file. var texture = ES3.LoadImage("myImage.png"); // Apply the Texture2D to the material on this object. GetComponent<Renderer>.material.mainTexture = texture; |
Load image from byte array
1 2 3 4 5 |
// Get the bytes of a PNG file from an external cloud service. byte[] bytes = CloudService.GetFileBytes("file.png"); // Turn these bytes into a Texture2D. var texture = ES3.LoadImage(bytes); |
Saving and loading audio
Loading audio files
You can load audio files as an AudioClip using the ES3.LoadAudio method.
As this method requires file access, this method is not supported on WebGL.
MP3 files are only supported on mobile, and Ogg Vorbis files are only supported on standalone platforms.
WAV, XM, IT, MOD and S3M files are supported on all platforms.
1 2 3 4 5 6 7 8 |
// Get the AudioSource we want to use to play our AudioClip. var source = this.GetComponent<AudioSource>(); // Load an AudioClip from the streaming assets folder into our source. source.clip = ES3.LoadAudio(Application.streamingAssetsPath + "/AudioFile.wav"); // Play the AudioClip we just loaded using our AudioSource. source.Play(); |
Saving audio files
It’s not possible to save an AudioClip to a compressed format as Unity lacks the required encoders to do so.
However, it’s possible to save and load an AudioClip in Easy Save’s format using the normal ES3.Save and ES3.Load methods. As the data is uncompressed, the file size will be larger than compressed formats.
1 2 3 4 5 6 7 8 9 |
// Get the AudioSource containing our AudioClip. var source = this.GetComponent<AudioSource>(); // Save an AudioClip in Easy Save's uncompressed format. ES3.Save<AudioClip>("myAudio", source.clip); // Load the AudioClip back into the AudioSource and play it. source.clip = ES3.Load<AudioClip>("myAudio"); source.Play(); |
1 |
ES3.SaveRaw( ES3.LoadRawBytes("audio.wav"), "audio.wav"); |