728x90
반응형
1. 네임스페이스
네임스페이스 |
using System; using System.Runtime.InteropServices; |
2. Byte 배열 → Struct
메서드 : Byte 배열 → Struct |
public T ByteArrayToStruct<T>(byte[] buffer) where T : struct { int size = Marshal.SizeOf(typeof(T)); if (size > buffer.Length) { throw new Exception(); } IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(buffer, 0, ptr, size); T obj = (T)Marshal.PtrToStructure(ptr, typeof(T)); Marshal.FreeHGlobal(ptr); return obj; } |
3. Struct → Byte 배열
메서드 : Struct → Byte 배열 |
public byte[] StructToByteArray(object obj) { int size = Marshal.SizeOf(obj); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(obj, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return arr; } |
728x90
반응형
'프로그램 > 유니티 스크립트 소스' 카테고리의 다른 글
[유니티 스크립트 소스] 마우스(Mouse) 관련 (0) | 2020.03.24 |
---|---|
[유니티 스크립트 소스] 마우스로 게임오브젝트 Drag로 이동시키기 (1) | 2020.03.15 |
[유니티 스크립트 소스] XKCDColors (0) | 2020.03.10 |
[유니티 스크립트 소스] Path 클래스 (0) | 2020.03.07 |
[유니티 스크립트 소스] 싱글톤(Singleton) (0) | 2020.03.07 |