Skip to content

FileStorage API

Local persistent storage for Worlds. Used for save data, configuration, cached world state, and access to raw user-provided files placed in the LocalStorage directory.

Files are stored at: /AppData/Local/ChilloutVR/LocalStorage/[WorldId]/[fileName]

Info

Only Worlds may use FileStorage. Avatars and Props cannot access FileStorage.

Implementation Details

  • Worlds must request user permission to use FileStorage via FileStorage.RequestUseFileStorage.
  • Files written by this API are sandboxed and encrypted; scripts never see the real filesystem.
  • This is done to prevent writing malicious files, not secure storage. The encryption key is stored in the header of each written file.
  • Files added manually to a world's LocalStorage folder aren't counted against the world's storage, are read-only, and gated by a user permission.

FileStorage

WasmScripting.FileStorage

FileStorage provides static methods for reading and writing files to persistent local storage for the current world.

Static Methods

Signature Description
CVRFile ReadFile(string fileName) Reads the entire file
CVRFile ReadFile(string fileName, int offset, int length) Reads a segment of a file
void WriteFile(string fileName, Span bytes) Writes all bytes (overwrite)
void WriteFile(string fileName, Span bytes, int offset) Writes bytes at a specific offset
void DeleteFile(string fileName) Deletes a file
void RenameFile(string oldFileName, string newFileName) Renames a file
bool FileExists(string fileName) Checks if a file exists
string[] GetFiles() Returns all file names
int GetFileSize(string fileName) Returns file size in bytes
long GetTotalSize() Total size of all stored files
long GetTotalCapacity() Maximum allowed storage size
bool CanUseFileStorage() Whether the world can use FileStorage
void RequestUseFileStorage(Action onResult) Prompts the user once for permission

CVRFile

WasmScripting.CVRFile

CVRFile represents a file returned by FileStorage.ReadFile.

Instance Properties

Member Type Description
Bytes Span The file's raw byte buffer
Length int Number of bytes returned

Bytes is a live span referencing unmanaged memory.
Do not store it long-term; copy data if needed.

Example: Writing and Reading Files

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