Instantly share code, notes, and snippets.

Embed
What would you like to do?
DynamicBufferに格納した複数のFloatを毎フレーム別スレッドで加算するだけ
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
public class ArrayDataComponent : MonoBehaviour
{
public TestBuffer[] originalBuffer;
Entity entity;
JobHandle handle;
void OnEnable ()
{
EntityManager em = World.Active.GetExistingManager<EntityManager>();
entity = em.CreateEntity(ComponentType.Create<TestBuffer>());
DynamicBuffer<TestBuffer> buffer = em.GetBuffer<TestBuffer>(entity);
buffer.CopyFrom(originalBuffer);
}
void OnDisable()
{
handle.Complete();
var em = World.Active.GetExistingManager<EntityManager>();
em.DestroyEntity(entity);
}
void Update ()
{
handle.Complete();
var em = World.Active.GetExistingManager<EntityManager>();
DynamicBuffer<TestBuffer> buffer = em.GetBuffer<TestBuffer>(entity);
handle = new AddJob() {
buffer = buffer.Reinterpret<float>().ToNativeArray()
}.Schedule(buffer.Length, 4, handle);
JobHandle.ScheduleBatchedJobs();
}
struct AddJob : IJobParallelFor
{
public NativeArray<float> buffer;
public void Execute(int i)
{
buffer[i] += 1f;
Debug.Log(buffer[i]);
}
}
}
[System.Serializable]
[InternalBufferCapacity(16)]
public struct TestBuffer : IBufferElementData
{
public float num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment