#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Graph Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.GraphDesigner.Runtime.Variables.ECS
{
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
///
/// Unmanaged buffer element storing shared variable values as raw bytes.
/// Supports any unmanaged type up to 16 bytes (float, int, bool, float2, float3, float4, etc.).
///
public struct SharedVariableElement : IBufferElementData
{
[Tooltip("16-byte storage that covers float, int, bool, double, float3, float4, and similar values.")]
public float4 Value;
}
///
/// Extension methods for reading and writing typed values in a SharedVariableElement buffer.
/// Burst-compatible via UnsafeUtility.MemCpy.
///
public static class SharedVariableBufferExtensions
{
///
/// Reads a value of type T from the buffer at the specified index.
/// T must be an unmanaged type no larger than 16 bytes.
///
/// The buffer that stores the shared variable values.
/// The index of the shared variable within the buffer.
/// The value stored at the specified buffer index.
public static unsafe T Get(this DynamicBuffer buffer, int index) where T : unmanaged
{
T result = default;
var element = buffer[index];
UnsafeUtility.MemCpy(&result, &element.Value, UnsafeUtility.SizeOf());
return result;
}
///
/// Writes a value of type T into the buffer at the specified index.
/// T must be an unmanaged type no larger than 16 bytes.
///
/// The buffer that stores the shared variable values.
/// The index of the shared variable within the buffer.
/// The value that should be written to the buffer.
public static unsafe void Set(this DynamicBuffer buffer, int index, T value) where T : unmanaged
{
var element = buffer[index];
UnsafeUtility.MemCpy(&element.Value, &value, UnsafeUtility.SizeOf());
buffer[index] = element;
}
}
}
#endif