Local Save Selection in Unity WebGL with Easy Save 3
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Local Save Selection in Unity WebGL with Easy Save 3
Hello,
I'm working on a Unity WebGL project, and I need to allow users to select from multiple save files stored locally on their computer. Each save contains several Texture2D assets, so these files can be quite large. For this reason, I’d prefer to avoid using a database to store the data as I have limited storage space.
Ideally, I want to let users browse their files with a file browser, select the save file, and then load it. The goal is to use something like ES3.Load<T>(key, filename);, but with a flexible filename that can point to a chosen file path that works in WebGL.
Is there any way to make this work, or are there alternative approaches for accessing local files in WebGL?
Thanks in advance for your help!
I'm working on a Unity WebGL project, and I need to allow users to select from multiple save files stored locally on their computer. Each save contains several Texture2D assets, so these files can be quite large. For this reason, I’d prefer to avoid using a database to store the data as I have limited storage space.
Ideally, I want to let users browse their files with a file browser, select the save file, and then load it. The goal is to use something like ES3.Load<T>(key, filename);, but with a flexible filename that can point to a chosen file path that works in WebGL.
Is there any way to make this work, or are there alternative approaches for accessing local files in WebGL?
Thanks in advance for your help!
Re: Local Save Selection in Unity WebGL with Easy Save 3
Hi there,
The WebGL specification does not allow access to the user's file system for security reasons. You may be able to create a Javascript plugin within the webpage hosting the WebGL application to get this data (see this thread on Unity Discussion for more info), but we wouldn't be able to support this ourselves.
All the best,
Joel
The WebGL specification does not allow access to the user's file system for security reasons. You may be able to create a Javascript plugin within the webpage hosting the WebGL application to get this data (see this thread on Unity Discussion for more info), but we wouldn't be able to support this ourselves.
All the best,
Joel
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Re: Local Save Selection in Unity WebGL with Easy Save 3
Thank you for the response, Joel!
I understand the limitations with WebGL and direct file system access. My main challenge is a bit different: I already have the save file data in a variable, `public byte[] byteResult;`, which contains the data from the save file. I also have a `UnityWebRequest webRequest` variable that I can use to fetch or manage this data if needed.
Is there any way I could use `ES3.Load<T>(key);` to read directly from `byteResult`, or perhaps integrate `webRequest` to help with this? I’m trying to find a way to load from a file directly instead of from these data sources.
Thanks again for your help!
I understand the limitations with WebGL and direct file system access. My main challenge is a bit different: I already have the save file data in a variable, `public byte[] byteResult;`, which contains the data from the save file. I also have a `UnityWebRequest webRequest` variable that I can use to fetch or manage this data if needed.
Is there any way I could use `ES3.Load<T>(key);` to read directly from `byteResult`, or perhaps integrate `webRequest` to help with this? I’m trying to find a way to load from a file directly instead of from these data sources.
Thanks again for your help!
Re: Local Save Selection in Unity WebGL with Easy Save 3
Hi there,
In this case you would use ES3.SaveRaw to save the data to the cache and then load from there.
I.e.
All the best,
Joel
In this case you would use ES3.SaveRaw to save the data to the cache and then load from there.
I.e.
Code: Select all
var settings = new ES3Settings(ES3.Location.Cache);
ES3.SaveRaw(bytes, settings);
vat data = ES3.Load<Type>("key", settings);
Joel
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Re: Local Save Selection in Unity WebGL with Easy Save 3
Thank you so much for the response,
Your solution works perfectly and does exactly what I needed. I really appreciate your help and the clarity of your explanation!
All the best,
William
Your solution works perfectly and does exactly what I needed. I really appreciate your help and the clarity of your explanation!
All the best,
William
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Re: Local Save Selection in Unity WebGL with Easy Save 3
I'm trying to use LoadInto. Before, we loaded the program in raw, with no compression. But because we are saving byte[], we needed to compress it. Currently, my program relies on the following method to load values:
The settings are configured like this:
We specifically want to use ES3.CompressionType.Gzip. Additionally, we are saving byte[] data in the save file because we need to handle Texture2D. However, if we try to switch to LoadInto, we encounter some errors.
Do you know how to resolve the issue while keeping the compression type, supporting byte[], and avoiding errors with LoadInto?
Thanks for your help
it's really appreciated!
Code: Select all
private T GetES3Value<T>(string key)
{
try
{
return ES3.Load<T>(key, settings);
// ES3.LoadInto<T>(key, settings);
}
catch (Exception err)
{
errMng.ExceptionError("SaveManager", "GetES3Value", err);
}
return default;
}
Code: Select all
settings = new ES3Settings(ES3.CompressionType.Gzip, ES3.Location.Cache);
Do you know how to resolve the issue while keeping the compression type, supporting byte[], and avoiding errors with LoadInto?
Thanks for your help
it's really appreciated!
Re: Local Save Selection in Unity WebGL with Easy Save 3
Hi there,
What errors are you getting? Also your ES3.LoadInto call isn't specifying an object to load the data into.
All the best,
Joel
What errors are you getting? Also your ES3.LoadInto call isn't specifying an object to load the data into.
All the best,
Joel
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Re: Local Save Selection in Unity WebGL with Easy Save 3
We are using Unity and webGL. We have many gameobjects in our scene that have a script component called ElevationData attached to them. There is a texture variable inside ElevationData which is populated when those gameObjects are spawned. When saving our data, we save all the values of the variables inside the ElevationData script instances.
When saving our data when using the Unity Editor, it correctly saves everything but problems starts to arise when we try to do the same thing in webGL : the texture seems to take too long to save which breaks the process and causes some informations to be lost.
Please note that we didn't have any problems before we started saving the texture in ElevationData, everything worked perfectly. We pinpointed the problem by saving in the Unity Editor and we then tried to load that save when using webGL and it correctly loaded so the problem really comes from saving the texture data in webGL.
We tried to resolve this problem by converting the texture into a bytes array right away when spawning the gameObjects so that we don't have to convert the texture into a bytes array when saving but the saving process seems to still be too slow. We also tried to add some "yield return new WaitForSeconds(0.1f)" inside our process to make sure that a step is fully completed before proceeding to the next one but it didn't work.
What could we do to resolve this?
This is our saving process :
When saving our data when using the Unity Editor, it correctly saves everything but problems starts to arise when we try to do the same thing in webGL : the texture seems to take too long to save which breaks the process and causes some informations to be lost.
Please note that we didn't have any problems before we started saving the texture in ElevationData, everything worked perfectly. We pinpointed the problem by saving in the Unity Editor and we then tried to load that save when using webGL and it correctly loaded so the problem really comes from saving the texture data in webGL.
We tried to resolve this problem by converting the texture into a bytes array right away when spawning the gameObjects so that we don't have to convert the texture into a bytes array when saving but the saving process seems to still be too slow. We also tried to add some "yield return new WaitForSeconds(0.1f)" inside our process to make sure that a step is fully completed before proceeding to the next one but it didn't work.
What could we do to resolve this?
This is our saving process :
Code: Select all
settings = new ES3Settings(ES3.Location.Cache);
public IEnumerator SaveElevations()
{
PageFormat pgFormat = ScriptManager.GetScript_PageFormat();
if (pgFormat == null)
yield return null;
List<string> arrayElevations = new List<string>();
if (pgFormat.AllElevations.Count > 0)
{
foreach (GameObject elevation in pgFormat.AllElevations)
{
SaveES3Elevations(elevation);
arrayElevations.Add(elevation.name);
}
}
if (arrayElevations.Count > 0)
SaveES3("ArrayElevations", arrayElevations.ToArray());
yield return null;
}
public void SaveES3Elevations(GameObject elevation)
{
MoveElevations moveElev = elevation.GetComponent<MoveElevations>();
if (moveElev == null)
return;
ElevationData elevData = elevation.GetComponent<ElevationData>();
if (elevData == null)
return;
SaveElevations SavedVariable = new SaveElevations();
SavedVariable.elevationInstanceName = elevData.elevationInstanceName;
SavedVariable.elevationObjName = elevation.name;
SavedVariable.elevPosition = moveElev.elevPosition;
SavedVariable.elevWidth = elevData.elevWidth;
SavedVariable.elevHeight = elevData.elevHeight;
SavedVariable.isInUnrestricted = moveElev.isInUnrestricted;
SavedVariable.PartitionName = elevData.PartitionName;
SavedVariable.isPlaced = elevData.isPlaced;
SavedVariable.textureBytes = elevData.textureBytes;
SavedVariable.textureWidth = elevData.textureWidth;
SavedVariable.textureHeight = elevData.textureHeight;
SavedVariable.originalWidth = elevData.originalWidth;
SavedVariable.originalHeight = elevData.originalHeight;
SavedVariable.croppedWidth = elevData.croppedWidth;
SavedVariable.croppedHeight = elevData.croppedHeight;
SavedVariable.ratio = elevData.ratio;
SavedVariable.nameCombo = elevData.nameCombo;
SaveES3("elevation_" + elevation.name, SavedVariable);
}
public struct SaveElevations
{
public string elevationObjName;
public string elevationInstanceName;
public Vector3 elevPosition;
public float elevWidth;
public float elevHeight;
public bool isInUnrestricted;
public string PartitionName;
public bool isPlaced;
public byte[] textureBytes;
public float textureWidth;
public float textureHeight;
public float originalWidth;
public float originalHeight;
public int croppedWidth;
public int croppedHeight;
public Vector2 ratio;
public string nameCombo;
}
public class ElevationData : MonoBehaviour
{
public string PartitionName;
public string elevationInstanceName;
public bool isPlaced;
public float elevWidth;
public float elevHeight;
public byte[] textureBytes;
public float textureWidth;
public float textureHeight;
public float originalWidth;
public float originalHeight;
public int croppedWidth;
public int croppedHeight;
public Vector2 ratio;
public string nameCombo;
}
Re: Local Save Selection in Unity WebGL with Easy Save 3
Hi there,
The issue you are having is likely due to the quantity of data you're saving, which is limited by the speed of your storage hardware rather than Easy Save.
Just to check, are you compressing your Texture before getting it's bytes? I.e. using Unity's Texture2D.EncodeToPNG method to get the bytes, and Texture2D.LoadImage to turn PNG bytes back to a Texture? This will reduce the quantity of data being saved.
All the best,
Joel
The issue you are having is likely due to the quantity of data you're saving, which is limited by the speed of your storage hardware rather than Easy Save.
Just to check, are you compressing your Texture before getting it's bytes? I.e. using Unity's Texture2D.EncodeToPNG method to get the bytes, and Texture2D.LoadImage to turn PNG bytes back to a Texture? This will reduce the quantity of data being saved.
All the best,
Joel
-
- Posts: 19
- Joined: Thu Nov 30, 2023 3:20 pm
Re: Local Save Selection in Unity WebGL with Easy Save 3
Hi Joel,
Thank you for your response.
Could you please clarify what you mean by "the speed of your storage hardware" and how it functions specifically in a WebGL context? We’re having some difficulty understanding where the data is stored in WebGL and how storage limitations are managed in this environment.
We’ll test the approach you mentioned regarding compressing the texture using Texture2D.EncodeToPNG and loading it back with Texture2D.LoadImage to see if it helps reduce the quantity of data being saved.
Thank you for your response.
Could you please clarify what you mean by "the speed of your storage hardware" and how it functions specifically in a WebGL context? We’re having some difficulty understanding where the data is stored in WebGL and how storage limitations are managed in this environment.
We’ll test the approach you mentioned regarding compressing the texture using Texture2D.EncodeToPNG and loading it back with Texture2D.LoadImage to see if it helps reduce the quantity of data being saved.