Skip to content

Example: BufferReaderWriter Usage

Example demonstrating how to use the BufferReaderWriter utility to write and read various data types.

 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
public partial class BufferReaderWriterExample : WasmBehaviour
{
    private void Start()
    {
        // Write some values
        BufferReaderWriter writer = new BufferReaderWriter();
        writer.Write(12345);
        writer.Write(3.14f);
        writer.Write("Hello Buffer");
        writer.Write(new int[] { 10, 20, 30 });

        Debug.Log($"Wrote {writer.Length} bytes into buffer.");

        // Read them back
        BufferReaderWriter reader = new BufferReaderWriter(writer.Buffer[..writer.Length]);

        reader.Read(out int intVal);
        reader.Read(out float floatVal);
        reader.Read(out string strVal);
        reader.Read(out int[] arrayVal);

        Debug.Log($"Int: {intVal}");
        Debug.Log($"Float: {floatVal}");
        Debug.Log($"String: {strVal}");
        Debug.Log($"Array: [{string.Join(", ", arrayVal)}]");
    }
}