Last active
September 15, 2020 20:10
-
-
Save alexzzzz/1186d0a84b5911a75101b248ef6646f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Runtime.InteropServices; | |
| class Program | |
| { | |
| [StructLayout(LayoutKind.Explicit)] | |
| struct Number | |
| { | |
| [FieldOffset(0)] | |
| public uint i; | |
| [FieldOffset(0)] | |
| public float f; | |
| } | |
| [StructLayout(LayoutKind.Explicit)] | |
| struct Color | |
| { | |
| [FieldOffset(0)] | |
| public byte r; | |
| [FieldOffset(1)] | |
| public byte g; | |
| [FieldOffset(2)] | |
| public byte b; | |
| [FieldOffset(3)] | |
| public byte a; | |
| [FieldOffset(0)] | |
| public uint rgba; | |
| } | |
| [StructLayout(LayoutKind.Explicit)] | |
| struct Vector | |
| { | |
| [FieldOffset(0)] | |
| public float x; | |
| [FieldOffset(4)] | |
| public float y; | |
| [FieldOffset(8)] | |
| public float z; | |
| [FieldOffset(0)] | |
| public unsafe fixed float items[3]; | |
| } | |
| static unsafe void Main() | |
| { | |
| var vector = default(Vector); | |
| vector.items[0] = 10; | |
| Console.WriteLine(vector.x); // 10 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment