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.
CloseFor 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.
Closetype | The type whose byte size is to be determined. |
int The total size in bytes of the specified type, including any alignment padding required for efficient memory access.
Determines the size, in bytes, of a specified type, including padding for alignment.
The SizeOf
method calculates the number of bytes needed to store a single instance of a provided type. This includes any padding necessary for proper memory alignment, which ensures efficient data access and hardware compliance.
Use this method when manually allocating memory or interfacing with lower-level systems in Unity. It is essential for tasks that require precise control over memory use, such as custom data structures or interaction with native plugins.
using UnityEngine; using Unity.Collections.LowLevel.Unsafe; using System.Runtime.InteropServices; using Unity.Collections;
public class SizeOfExample : MonoBehaviour { [StructLayout(LayoutKind.Sequential)] struct ComplexStruct { public byte SmallValue; public double LargeValue; public short MediumValue; }
void Start() { // Calculate and log the size of ComplexStruct int structSize = UnsafeUtility.SizeOf<ComplexStruct>(); Debug.Log($"Size of ComplexStruct: {structSize} bytes");
// Using SizeOf to allocate memory for an array of ComplexStruct unsafe { void* structBuffer = UnsafeUtility.Malloc(structSize * 5, UnsafeUtility.AlignOf<ComplexStruct>(), Allocator.Temp);
// Cast the buffer to work with the ComplexStruct type ComplexStruct* structArray = (ComplexStruct*)structBuffer; for (int i = 0; i < 5; i++) { structArray[i].SmallValue = (byte)i; structArray[i].LargeValue = i * 2.2; structArray[i].MediumValue = (short)(i * 10); Debug.Log($"Struct[{i}]: SmallValue = {structArray[i].SmallValue}, LargeValue = {structArray[i].LargeValue}, MediumValue = {structArray[i].MediumValue}"); }
// Free the allocated memory UnsafeUtility.Free(structBuffer, Allocator.Temp); } } }
Additional resources: UnsafeUtility.MallocTracked UnsafeUtility.AlignOf.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.