DynamicBufferに格納した複数のFloatを毎フレーム別スレッドで加算するだけ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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