10using System.Runtime.InteropServices;
17 public static class StructMarshal<T> where T : struct {
19 [StructLayout(LayoutKind.Sequential)]
20 private class StructContainer {
25 private static StructContainer _container;
28 private static int _sizeofT;
30 static StructMarshal() {
31 _sizeofT = Marshal.SizeOf(typeof(T));
38 public static int Size {
47 public static void PtrToStruct(IntPtr ptr, out T t) {
49#if UNITY_2018_1_OR_NEWER
51 Unity.Collections.LowLevel.Unsafe.UnsafeUtility.CopyPtrToStructure((
void*)ptr, out t);
54#error UnityModules Only supports IL2CPP on versions of Unity 2018.1 or greater.
57 if (_container ==
null) {
58 _container =
new StructContainer();
62 Marshal.PtrToStructure(ptr, _container);
64 }
catch (Exception e) {
76 public static void ArrayElementToStruct(IntPtr ptr,
int arrayIndex, out T t) {
77 PtrToStruct(
new IntPtr(ptr.ToInt64() + _sizeofT * arrayIndex), out t);