docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Struct FastBufferReader

    Optimized class used for reading values from a byte stream FastBufferWriter BytePacker ByteUnpacker

    Implements
    IDisposable
    Inherited Members
    ValueType.Equals(object)
    ValueType.GetHashCode()
    ValueType.ToString()
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetType()
    Namespace: Unity.Netcode
    Assembly: solution.dll
    Syntax
    public struct FastBufferReader : IDisposable

    Constructors

    Name Description
    FastBufferReader(ArraySegment<byte>, Allocator, int, int)

    Create a FastBufferReader from an ArraySegment.

    A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

    Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block and ensure the FastBufferReader isn't used outside that block.

    FastBufferReader(byte*, Allocator, int, int, Allocator)

    Create a FastBufferReader from an existing byte buffer.

    A new buffer will be created using the given and the value will be copied in. FastBufferReader will then own the data.

    The exception to this is when the passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory). This is true, unless the param is explicitly set to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller should manually call Dispose() when it is no longer needed.

    FastBufferReader(byte[], Allocator, int, int)

    Create a FastBufferReader from an existing byte array.

    A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

    Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block and ensure the FastBufferReader isn't used outside that block.

    FastBufferReader(NativeArray<byte>, Allocator, int, int, Allocator)

    Create a FastBufferReader from a NativeArray.

    A new buffer will be created using the given and the value will be copied in. FastBufferReader will then own the data.

    The exception to this is when the passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory). This is true, unless the param is explicitly set to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller should manually call Dispose() when it is no longer needed.

    FastBufferReader(FastBufferReader, Allocator, int, int, Allocator)

    Create a FastBufferReader from another existing FastBufferReader. This is typically used when you want to change the copyAllocator that a reader is allocated to - for example, upgrading a Temp reader to a Persistent one to be processed later.

    A new buffer will be created using the given and the value will be copied in. FastBufferReader will then own the data.

    The exception to this is when the passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory).

    FastBufferReader(FastBufferWriter, Allocator, int, int, Allocator)

    Create a FastBufferReader from a FastBufferWriter.

    A new buffer will be created using the given and the value will be copied in. FastBufferReader will then own the data.

    The exception to this is when the passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory). This is true, unless the param is explicitly set to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller should manually call Dispose() when it is no longer needed.

    Properties

    Name Description
    IsInitialized

    Gets a value indicating whether the reader has been initialized and a handle allocated.

    Length

    Get the total length of the buffer

    Position

    Get the current read position

    Methods

    Name Description
    Dispose()

    IDisposable implementation that frees the allocated buffer

    EnterBitwiseContext()

    Retrieve a BitReader to be able to perform bitwise operations on the buffer. No bytewise operations can be performed on the buffer until bitReader.Dispose() has been called. At the end of the operation, FastBufferReader will remain byte-aligned.

    GetUnsafePtr()

    Gets a direct pointer to the underlying buffer

    GetUnsafePtrAtCurrentPosition()

    Gets a direct pointer to the underlying buffer at the current read position

    ReadByte(out byte)

    Read a byte to the stream.

    ReadByteSafe(out byte)

    Read a byte to the stream.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadBytes(byte*, int, int)

    Read multiple bytes to the stream

    ReadBytes(ref byte[], int, int)

    Read multiple bytes from the stream

    ReadBytesSafe(byte*, int, int)

    Read multiple bytes to the stream

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadBytesSafe(ref byte[], int, int)

    Read multiple bytes from the stream

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadNetworkSerializableInPlace<T>(ref T)

    Read an INetworkSerializable in-place, without constructing a new one Note that this will NOT check for null before calling NetworkSerialize

    ReadNetworkSerializable<T>(out NativeArray<T>, Allocator)

    Read a NativeArray of INetworkSerializables

    ReadNetworkSerializable<T>(out T)

    Read an INetworkSerializable

    ReadNetworkSerializable<T>(out T[])

    Read an array of INetworkSerializables

    ReadPartialValue<T>(out T, int, int)

    Read a partial value. The value is zero-initialized and then the specified number of bytes is read into it.

    ReadValue(out string, bool)

    Reads a string NOTE: ALLOCATES

    ReadValue(out Color32)

    Read a Color32

    ReadValue(out Color32[])

    Read a Color32 array

    ReadValue(out Color)

    Read a Color

    ReadValue(out Color[])

    Read a Color array

    ReadValue(out Quaternion)

    Read a Quaternion

    ReadValue(out Quaternion[])

    Read a Quaternion array

    ReadValue(out Ray2D)

    Read a Ray2D

    ReadValue(out Ray2D[])

    Read a Ray2D array

    ReadValue(out Ray)

    Read a Ray

    ReadValue(out Ray[])

    Read a Ray array

    ReadValue(out Vector2)

    Read a Vector2

    ReadValue(out Vector2Int)

    Read a Vector2Int

    ReadValue(out Vector2Int[])

    Read a Vector2Int array

    ReadValue(out Vector2[])

    Read a Vector2 array

    ReadValue(out Vector3)

    Read a Vector3

    ReadValue(out Vector3Int)

    Read a Vector3Int

    ReadValue(out Vector3Int[])

    Read a Vector3Int array

    ReadValue(out Vector3[])

    Read a Vector3 array

    ReadValue(out Vector4)

    Read a Vector4

    ReadValue(out Vector4[])

    Read a Vector4

    ReadValueSafe(out string, bool)

    Reads a string. NOTE: ALLOCATES

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Color32)

    Read a Color32

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Color32[])

    Read a Color32 array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Color)

    Read a Color

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Color[])

    Read a Collor array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Quaternion)

    Read a Quaternion

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Quaternion[])

    Read a Quaternion array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Ray2D)

    Read a Ray2D

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Ray2D[])

    Read a Ray2D array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Ray)

    Read a Ray

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Ray[])

    Read a Ray array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector2)

    Read a Vector2

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector2Int)

    Read a Vector2Int

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector2Int[])

    Read a Vector2Int array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector2[])

    Read a Vector2 array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector3)

    Read a Vector3

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector3Int)

    Read a Vector3Int

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector3Int[])

    Read a Vector3Int array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector3[])

    Read a Vector3 array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector4)

    Read a Vector4

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe(out Vector4[])

    Read a Vector4 array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafeInPlace<T>(ref T, ForFixedStrings)

    Read a FixedString value.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafeTemp<T>(out NativeArray<T>)

    Read a FixedString NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafeTemp<T>(out NativeArray<T>, ForGeneric)

    Read a struct NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out NativeArray<T>, Allocator)

    Read a FixedString NativeArray.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out NativeArray<T>, Allocator, ForGeneric)

    Read a struct NativeArray

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out NativeArray<T>, Allocator, ForNetworkSerializable)

    Read a NetworkSerializable NativeArray

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T, ForEnums)

    Read an enum value

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T, ForFixedStrings)

    Read a FixedString value.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T, ForNetworkSerializable)

    Read a NetworkSerializable value

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T, ForPrimitives)

    Read a primitive value (int, bool, etc) Accepts any value that implements the given interfaces, but is not guaranteed to work correctly on values that are not primitives.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T, ForStructs)

    Read a struct

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T[], ForEnums)

    Read an enum array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T[], ForFixedStrings)

    Read a FixedString NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T[], ForNetworkSerializable)

    Read a NetworkSerializable array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T[], ForPrimitives)

    Read a primitive value (int, bool, etc) array Accepts any value that implements the given interfaces, but is not guaranteed to work correctly on values that are not primitives.

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueSafe<T>(out T[], ForStructs)

    Read a struct array

    "Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

    ReadValueTemp<T>(out NativeArray<T>, ForGeneric)

    Read a struct NativeArray using a Temp allocator. Equivalent to ReadValue(out value, Allocator.Temp)

    ReadValue<T>(out NativeArray<T>, Allocator, ForGeneric)

    Read a struct NativeArray

    ReadValue<T>(out T, ForEnums)

    Read an enum value

    ReadValue<T>(out T, ForFixedStrings)

    Read a FixedString value. This method is a little difficult to use, since you have to know the size of the string before reading it, but is useful when the string is a known, fixed size. Note that the size of the string is also encoded, so the size to call TryBeginRead on is actually the fixed size (in bytes) plus sizeof(int)

    ReadValue<T>(out T, ForNetworkSerializable)

    Read a NetworkSerializable value

    ReadValue<T>(out T, ForPrimitives)

    Read a primitive value (int, bool, etc) Accepts any value that implements the given interfaces, but is not guaranteed to work correctly on values that are not primitives.

    ReadValue<T>(out T, ForStructs)

    Read a struct

    ReadValue<T>(out T[], ForEnums)

    Read an enum array

    ReadValue<T>(out T[], ForNetworkSerializable)

    Read a NetworkSerializable array

    ReadValue<T>(out T[], ForPrimitives)

    Read a primitive value array (int, bool, etc) Accepts any value that implements the given interfaces, but is not guaranteed to work correctly on values that are not primitives.

    ReadValue<T>(out T[], ForStructs)

    Read a struct array

    Seek(int)

    Move the read position in the stream

    ToArray()

    Returns an array representation of the underlying byte buffer. !!Allocates a new array!!

    TryBeginRead(int)

    Allows faster serialization by batching bounds checking. When you know you will be reading multiple fields back-to-back and you know the total size, you can call TryBeginRead() once on the total size, and then follow it with calls to ReadValue() instead of ReadValueSafe() for faster serialization.

    Unsafe read operations will throw OverflowException in editor and development builds if you go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following operations in release builds.

    TryBeginReadValue<T>(in T)

    Allows faster serialization by batching bounds checking. When you know you will be reading multiple fields back-to-back and you know the total size, you can call TryBeginRead() once on the total size, and then follow it with calls to ReadValue() instead of ReadValueSafe() for faster serialization.

    Unsafe read operations will throw OverflowException in editor and development builds if you go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following operations in release builds.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)