Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using Unity.Transforms;
using Unity.Rendering;
public class ChunkIterationBootstrap : MonoBehaviour
{
[SerializeField] MeshInstanceRenderer meshInstanceRenderer;
void Start()
{
var em = World.Active.GetOrCreateManager<EntityManager>();
var manpower = em.CreateArchetype(ComponentType.Create<MeshInstanceRenderer>(), ComponentType.Create<Position>(), ComponentType.Create<ManPowerData>());
var enginePower = em.CreateArchetype(ComponentType.Create<MeshInstanceRenderer>(), ComponentType.Create<Position>(), ComponentType.Create<EngineData>());
using (NativeArray<Entity> entities = new NativeArray<Entity>(15, Allocator.Temp, NativeArrayOptions.UninitializedMemory))
{
em.CreateEntity(manpower, entities);
for (int i = 0; i < entities.Length; i++)
em.SetSharedComponentData(entities[i], meshInstanceRenderer);
}
using (NativeArray<Entity> entities = new NativeArray<Entity>(15, Allocator.Temp, NativeArrayOptions.UninitializedMemory))
{
em.CreateEntity(enginePower, entities);
for (int i = 0; i < entities.Length; i++)
em.SetSharedComponentData(entities[i], meshInstanceRenderer);
}
}
}
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.Transforms;
using Unity.Burst;
public class ChunkIterationTest : JobComponentSystem
{
ComponentGroup group;
protected override void OnCreateManager()
{
group = GetComponentGroup(new EntityArchetypeQuery()
{
All = new ComponentType[] {
ComponentType.Create<Position>()
},
Any = new ComponentType[] {
ComponentType.ReadOnly<ManPowerData>(),
ComponentType.ReadOnly<EngineData>()
},
None = System.Array.Empty<ComponentType>()
});
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new MyJob()
{
typeManPower = GetArchetypeChunkComponentType<ManPowerData>(true),
typeEngine = GetArchetypeChunkComponentType<EngineData>(true),
typePosition = GetArchetypeChunkComponentType<Position>(false),
};
return job.Schedule(group, inputDeps);
}
[BurstCompile]
struct MyJob : IJobChunk
{
[ReadOnly] public ArchetypeChunkComponentType<ManPowerData> typeManPower;
[ReadOnly] public ArchetypeChunkComponentType<EngineData> typeEngine;
public ArchetypeChunkComponentType<Position> typePosition;
public void Execute(ArchetypeChunk chunk, int chunkIndex)
{
var array = chunk.GetNativeArray(typePosition);
if (chunk.Has(typeManPower))
ProcessManPower(array);
else if (chunk.Has(typeEngine))
ProcessEngine(array);
}
void ProcessManPower(NativeArray<Position> array)
{
for (int i = 0; i < array.Length; i++)
{
var pos = array[i];
pos.Value.y += 0.1f;
array[i] = pos;
}
}
void ProcessEngine(NativeArray<Position> array)
{
for (int i = 0; i < array.Length; i++)
{
var pos = array[i];
pos.Value.y -= 0.1f;
array[i] = pos;
}
}
}
}
struct ManPowerData : IComponentData { }
struct EngineData : IComponentData { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment