Skip to content

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] RenderMesh meshInstanceRenderer;
void Start()
{
var em = World.Active.EntityManager;
// 2種類のEntityを用意
var manpower = em.CreateArchetype(
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadWrite<ManPowerData>());
var enginePower = em.CreateArchetype(
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadWrite<EngineData>());
// entityを生成
using (NativeArray<Entity> entities = new NativeArray<Entity>(15, Allocator.Temp))
{
em.CreateEntity(manpower, entities);
}
using (NativeArray<Entity> entities = new NativeArray<Entity>(8, Allocator.Temp))
{
em.CreateEntity(enginePower, entities);
}
}
}
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.Transforms;
using Unity.Burst;
public class ChunkIterationTest : JobComponentSystem
{
EntityQuery query;
protected override void OnCreateManager()
{
query = GetEntityQuery(new EntityQueryDesc()
{
All = new ComponentType[] {
ComponentType.ReadWrite<Translation>()
},
Any = new ComponentType[] {
ComponentType.ReadOnly<ManPowerData>(),
ComponentType.ReadOnly<EngineData>()
},
None = System.Array.Empty<ComponentType>()
});
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
return new MyJob
{
typeManPower = GetArchetypeChunkComponentType<ManPowerData>(true),
typeEngine = GetArchetypeChunkComponentType<EngineData>(true),
typePosition = GetArchetypeChunkComponentType<Translation>(false),
}.Schedule(query, inputDeps);
}
struct MyJob : IJobChunk
{
[ReadOnly] public ArchetypeChunkComponentType<ManPowerData> typeManPower;
[ReadOnly] public ArchetypeChunkComponentType<EngineData> typeEngine;
public ArchetypeChunkComponentType<Translation> typePosition;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var array = chunk.GetNativeArray(typePosition);
if (chunk.Has(typeManPower))
ProcessManPower(array);
else if (chunk.Has(typeEngine))
ProcessEngine(array);
}
void ProcessManPower(NativeArray<Translation> array)
{
for (int i = 0; i < array.Length; i++)
{
Translation pos = array[i];
pos.Value.y += 0.1f;
array[i] = pos;
}
UnityEngine.Debug.Log($"do man power * {array.Length}");
}
void ProcessEngine(NativeArray<Translation> array)
{
for (int i = 0; i < array.Length; i++)
{
Translation pos = array[i];
pos.Value.y -= 0.1f;
array[i] = pos;
}
UnityEngine.Debug.Log($"do engine power * {array.Length}");
}
}
}
struct ManPowerData : IComponentData { }
struct EngineData : IComponentData { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.