Game.Serialization.WriteBuffer
Assembly:
Game
Namespace: Game.Serialization
Type:
class
Base:
IWriteBuffer, IDisposable
Summary:
A simple write buffer used for serialization that stores bytes in a Unity.Collections.NativeList
Fields
-
private Unity.Jobs.JobHandle m_WriteDependencies
Tracks combined job dependencies (as a JobHandle) that must be completed before the buffer can be considered fully finished when m_HasDependencies is true. The handle is combined via JobHandle.CombineDependencies when Done(JobHandle) is called. -
private bool m_HasDependencies
Flag indicating whether there are any outstanding job dependencies stored in m_WriteDependencies. -
private bool m_IsDone
Flag indicating whether the buffer has been marked as done (via Done() or Done(JobHandle)). When false, isCompleted will always be false.
Properties
-
public Unity.Collections.NativeList<byte> buffer { get; private set; }
The underlying NativeListused to hold serialized bytes. It is allocated with Allocator.Persistent in the constructor. The setter is private; callers can read and mutate the list (Add, AddRange, etc.) but must not forget to Dispose the WriteBuffer to free the native memory. -
public bool isCompleted { get; }
Returns true when the buffer has been marked done and either: - No job dependencies were registered (m_HasDependencies is false) — simply m_IsDone == true, or
- Job dependencies were registered — m_IsDone == true and the combined m_WriteDependencies.IsCompleted is true. If m_IsDone is false the property always returns false.
Constructors
public WriteBuffer()
Initializes a new WriteBuffer and allocates the internal NativeListwith Allocator.Persistent.
Methods
-
public void CompleteDependencies()
If m_HasDependencies is true, calls m_WriteDependencies.Complete() to block until all combined jobs have finished, then resets m_WriteDependencies to default(JobHandle) and clears m_HasDependencies. Use this when you need to ensure any jobs writing into or depending on the buffer are finished before accessing or disposing the buffer. -
private void DisposeBuffers()
Internal cleanup helper that first completes any outstanding dependencies (via CompleteDependencies) and then disposes the internal NativeListif it is created. After disposing, it assigns the (now-disposed) native list back to the buffer field (preserving the property but the list will no longer be created). -
public void Dispose()
Implements IDisposable. Calls DisposeBuffers() to complete dependencies and free the NativeList. Call this when the buffer is no longer needed to avoid native memory leaks. -
public void Done(JobHandle handle)
Marks the buffer as done and combines the provided JobHandle with any existing write dependencies (via JobHandle.CombineDependencies). Sets m_HasDependencies to true and m_IsDone to true. Use this when finishing an async write job and you want the buffer to remain valid until the supplied job(s) complete. -
public void Done()
Marks the buffer as done without registering any job dependencies (sets m_IsDone = true). Use this when the buffer is finished synchronously and no job handles need tracking.
Usage Example
// Create and fill buffer
var writeBuffer = new Game.Serialization.WriteBuffer();
writeBuffer.buffer.Add(0x01);
writeBuffer.buffer.Add(0x02);
// If you schedule a job that writes/uses the buffer, capture its JobHandle:
JobHandle someJobHandle = /* schedule job that writes/reads buffer */;
// Mark buffer as done and attach job dependency so buffer will only be considered completed when job finishes
writeBuffer.Done(someJobHandle);
// Later, from a thread that needs the buffer to be finished:
if (!writeBuffer.isCompleted)
{
// Block until job dependencies complete
writeBuffer.CompleteDependencies();
}
// Use buffer.buffer contents here...
// When finished with the buffer, dispose to free native memory
writeBuffer.Dispose();
Notes and recommendations: - Because buffer is allocated with Allocator.Persistent, always call Dispose() (or use appropriate lifetime management) to avoid leaking native memory. - If you call Done(JobHandle), prefer to call CompleteDependencies() before disposing or accessing the buffer on the main thread to ensure all job work is finished. - This class does not perform thread-safety beyond JobHandle/Complete semantics; access to buffer from multiple threads must follow Unity's native container safety rules.