Skip to content

Example: Writing a Text File

Write and read a simple text file using the File Storage API and Buffer Reader Writer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using UnityEngine;
using WasmScripting;
using CVR;

public partial class FileStorageExample : WasmBehaviour
{
    void Start()
    {
        // Write file
        string dataToWrite = "Hello, FileStorage!";
        byte[] bytesToWrite = System.Text.Encoding.UTF8.GetBytes(dataToWrite);
        FileStorage.WriteFile("test", bytesToWrite);

        // Read file
        CVRFile file = FileStorage.ReadFile("test");

        if (file.Length > 0)
        {
            string content = System.Text.Encoding.UTF8.GetString(file.Bytes);
            Debug.Log($"Read from FileStorage: {content}");
        }
        else
        {
            Debug.Log("No data found in FileStorage for 'test'");
        }
    }
}