-
Notifications
You must be signed in to change notification settings - Fork 0
File loading help
To load a text or binary file from StreamingAssets you MUST use the FileLoader singleton - this allows us to build to WebGL without errors. Note that no file WRITING can be performed. To save data, please use Firebase.
File loading is performed asynchronously, so please keep this in mind when writing your systems that rely on data loaded from disk. Your systems must await the data before acting on it.
To load a binary file, call FileLoader.Instance.LoadAsBytes. This function takes in a string for the file path (Application.streamingAssetsPath + "your path in streaming assets") and a callback function. As LoadAsBytes is a coroutine, you must call the function with StartCoroutine.
In your code the callback function must have one parameter of type byte[]. When the file has been loaded using the LoadAsBytes function, the file contents will be passed to this callback function in the byte[] parameter.
Example code:
MemoryStream file_content = null;
private void Start() {
StartCoroutine(FileLoader.Instance.LoadAsBytes(Application.streamingAssetsPath + "myFile.bin", LoadCallback));
while (file_content == null || !file_content.CanRead)
yield return new WaitForEndOfFrame();
//Read from file_content with BinaryReader
}
private void LoadCallback(byte[] content)
{
file_content = new MemoryStream(content);
}To load a text file, call FileLoader.Instance.LoadAsText. This function takes in a string for the file path (Application.streamingAssetsPath + "your path in streaming assets") and a callback function. As LoadAsText is a coroutine, you must call the function with StartCoroutine.
In your code the callback function must have one parameter of type string. When the file has been loaded using the LoadAsText function, the file contents will be passed to this callback function in the string parameter.
Example code:
string file_content = "";
private void Start() {
StartCoroutine(FileLoader.Instance.LoadAsText(Application.streamingAssetsPath + "myFile.txt", LoadCallback));
while (file_content == "")
yield return new WaitForEndOfFrame();
//Use content from file_content
}
private void LoadCallback(string content)
{
file_content = content;
}