NativeListは要素を増減出来るという点で言えば便利な機能です。ただしIJobを使用している場合には大体の機能を使えるのですがIJobParallelForで書込を行おうとすると、InvalidOperationException: The previously scheduled job Job名 reads from the NativeArray NativeList名. You must call JobHandle.Complete() のようなエラーが表示されて実行できません。
struct AddJob : IJob
{
public NativeQueue<int> queue;
public NativeList<int> list;
publicvoid Execute()
{
while (queue.TryDequeue(outint item))
{
if (item % 2 == 0) // 2で割り切れる要素だけ追加
list.Add(item);
}
}
}
呼び出す側
// Listに登録する要素を
NativeQueue<int> queue = new NativeQueue<int>(Allocator.TempJob);
queue.Enqueue(3); queue.Enqueue(2);
queue.Enqueue(4); queue.Enqueue(5);
NativeList<int> list = new NativeList<int>(8, Allocator.TempJob);
// 内容の更新
var handle = new AddJob { queue = queue, list = list }.Schedule();
public NativeArray<T> ToComponentDataArray<T>(Allocator allocator, out JobHandle jobhandle) where T : struct,IComponentData {...}
publicvoid CopyFromComponentDataArray<T>(NativeArray<T> componentDataArray, out JobHandle jobhandle) where T : struct,IComponentData {...}