Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created May 20, 2018 04:00
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/695de3f807dcd84cd13e358782582cd9 to your computer and use it in GitHub Desktop.
Save tsubaki/695de3f807dcd84cd13e358782582cd9 to your computer and use it in GitHub Desktop.
unsafeでNativeArrayの要素に直接アクセスする
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
public sealed class RefTest : MonoBehaviour
{
struct Data
{
public float value;
}
unsafe void Start ()
{
NativeArray<Data> array = new NativeArray<Data>(10, Allocator.Temp);
var ptr = (Data*)array.GetUnsafePtr();
for(int i=0; i<10; i++)
{
Do(ptr + i);
}
foreach( var element in array)
{
Debug.LogFormat("{0:00}, {1:00.0}", element.value);
}
array.Dispose();
}
unsafe void Do(Data* p)
{
p->value = Random.Range(0f, 10f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment