Version: Unity 6.3 Beta (6000.3)
LanguageEnglish
  • C#

BufferSlice<T0>.SafeReinterpret

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public BufferSlice<U> SafeReinterpret();

Returns

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

Description

Reinterpret the slice as having a different data type (type punning), performing checks to ensure the reinterpret is valid.

Safely reinterprets the buffer slice to view the same memory as a different type. This method performs validation to ensure the type conversion is valid:

Validation Rules:

  • The size of the target type must be a multiple of the size of the current type.
  • The memory alignment requirements must be satisfied.
  • The reinterpretation must not exceed buffer boundaries.

This is useful for viewing the same data through different type lenses, such as treating a float buffer as a Vector3 buffer.

Additional resources: UnsafeReinterpret for unchecked reinterpretation.

// Create a buffer of floats
BufferID buffer = context.CreateBuffer(300, sizeof(float)); // 300 floats
var floatSlice = new BufferSlice<float>(buffer, 0);

// Safely reinterpret as Vector3 (3 floats = 1 Vector3) var vectorSlice = floatSlice.SafeReinterpret<Vector3>(); // Now views 100 Vector3s

// Write Vector3 data through the reinterpreted slice var vectorData = new NativeArray<Vector3>(100, Allocator.Temp); for (int i = 0; i < 100; i++) vectorData[i] = new Vector3(i, i + 0.5f, i + 1.0f);

context.WriteBuffer(vectorSlice, vectorData);

// The same memory can still be read as floats var floatData = new NativeArray<float>(300, Allocator.Temp); context.ReadBuffer(floatSlice, floatData);
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961