Skip to content

Instantly share code, notes, and snippets.

@alexzzzz
Last active September 15, 2020 20:10
Show Gist options
  • Select an option

  • Save alexzzzz/1186d0a84b5911a75101b248ef6646f2 to your computer and use it in GitHub Desktop.

Select an option

Save alexzzzz/1186d0a84b5911a75101b248ef6646f2 to your computer and use it in GitHub Desktop.
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