Create a gist now

Instantly share code, notes, and snippets.

Embed
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