Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


UnsafeReinterpret<U>()

Reinterpret the slice as having a different data type (type punning), but does not check if the reinterpret is valid.
Read time 1 minuteLast updated 13 days ago

Definition

UnsafeReinterpret<T>()

public BufferSlice<U> UnsafeReinterpret<U>() where U : struct

Returns

Type

Description

BufferSlice<T>An alias of the same slice, but reinterpreted as the target type.

Remarks

Performs unchecked reinterpretation of the buffer slice to view the same memory as a different type. This method skips all validation and should only be used when you are certain the reinterpretation is valid.
Use Cases:
  • Performance-critical code where validation overhead is unacceptable.
  • Advanced scenarios where you need to bypass safety checks.
  • Custom type layouts that don't follow standard alignment rules.
Warning: This method can lead to undefined behavior, memory corruption, or crashes if used incorrectly. Prefer BufferSlice<T>.SafeReinterpret unless you have specific performance requirements.
Additional Resources: BufferSlice<T>.SafeReinterpret for validated reinterpretation.

Examples

// Performance-critical reinterpretationvar floatSlice = new BufferSlice<float>(buffer, 0);// Unsafe reinterpret for maximum performance// WARNING: No validation - ensure this is safe!var byteSlice = floatSlice.UnsafeReinterpret<byte>();// Use with caution - misaligned access could crashvar intSlice = floatSlice.UnsafeReinterpret<int>(); // OK: same sizevar doubleSlice = floatSlice.UnsafeReinterpret<double>(); // DANGEROUS: different size