AddressOf<T>(ref T)
Obtains the memory address of the specified object as a pointer.
Read time 1 minuteLast updated 17 days ago
Definition
- Type: Method
- Namespace: Unity.Collections.LowLevel.Unsafe
- Assembly: UnityEngine.CoreModule
AddressOf<T>(T)
public static void* AddressOf<T>(ref T output) where T : struct
Parameters
Returns
Type | Description |
|---|---|
| void* | A void pointer that represents the memory address of the object. This pointer allows direct access to the memory location. Manage this pointer carefully to prevent runtime errors. |
Remarks
The method retrieves the memory address of a managed object. Use this in scenarios that need direct memory access or interaction with unmanaged code, where efficiency is crucial.
AddressOfExercise caution when you use this method. Improper handling of memory addresses may cause memory corruption or application instability. Use this method to enhance performance in areas where managed memory safety has significant overhead.
using Unity.Collections.LowLevel.Unsafe;using UnityEngine;public class AddressOfExample : MonoBehaviour{ struct ExampleStruct { public int Number; } void Start() { ExampleStruct example = new ExampleStruct { Number = 100 }; unsafe { // Get the memory address of the struct void* address = UnsafeUtility.AddressOf(ref example); // Modify the struct's value directly through the pointer ExampleStruct* pointer = (ExampleStruct*)address; pointer->Number = 200; Debug.Log($"Modified value: {example.Number}"); // Outputs: Modified value: 200 } }}
Additional Resources: UnsafeUtility.As.