Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active May 16, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/cb67b9e241eeac3ff095f4cc663426a7 to your computer and use it in GitHub Desktop.
Save tsubaki/cb67b9e241eeac3ff095f4cc663426a7 to your computer and use it in GitHub Desktop.
using Unity.IL2CPP.CompilerServices;
using UnityEngine;
[Il2CppSetOption(Option.NullChecks, false)]
unsafe sealed class UnsafeSampleBehaviour : MonoBehaviour
{
struct Point
{
public int x;
public int y;
}
Point[] points = new Point[]
{
new Point(){y = 1},
new Point(){y = 2},
new Point(){y = 3},
};
unsafe void Awake()
{
// 配列のアドレスを固定
int length = points.Length;
fixed (Point* px = points)
{
// アクセス先をズラして構造体の中身を直接書き換えていく
for (Point* p = px; p != px + length; ++p)
{
p->x = p->y;
}
}
// 特定の構造体の中身を書き換える
fixed (Point* p = &points[2]) { p->x = 12; }
}
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
void Start()
{
for (int i = 0; i < points.Length; i++)
Debug.Log(points[i].x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment