public class RotatorGO : IComponentData
{
public GameObject Value;
public RotatorGO(GameObject value)
{ Value = value;
}
// Every IComponentData class must have a no-arg constructor.
public RotatorGO()
{ }}
Simulate System Query ManaedComponentData 并更新
foreach (var (transform, speed, go) in
SystemAPI.Query<RefRW<LocalTransform>, RefRO<RotationSpeed>, RotatorGO>())
{
transform.ValueRW = transform.ValueRO.RotateY(
speed.ValueRO.RadiansPerSecond * deltaTime);
// Update the associated GameObject's transform to match.
go.Value.transform.rotation = transform.ValueRO.Rotation;
}
JobQuery
JobQuery 中有一些特别的用法
// On System
EntityTypeHandle = SystemAPI.GetEntityTypeHandle()
OtherChunks = boxQuery.ToArchetypeChunkArray(state.WorldUpdateAllocator)
LocalTransformTypeHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(true),
// On JobChunk
var entities = chunk.GetNativeArray(EntityTypeHandle);
var transforms = chunk.GetNativeArray(ref LocalTransformTypeHandle);
JobEntity
MoveProjectilesSystem.cs
[BurstCompile]
public partial struct MoveJob : IJobEntity
{
public float TimeSinceLoad;
public float ProjectileSpeed;
public EntityCommandBuffer.ParallelWriter ECBWriter;
void Execute(Entity projectileEntity, [ChunkIndexInQuery] int chunkIndex, ref LocalTransform transform,
in Projectile projectile)
{ float aliveTime = TimeSinceLoad - projectile.SpawnTime;
if (aliveTime > 5.0f)
{ ECBWriter.DestroyEntity(chunkIndex, projectileEntity);
} transform.Position.x = projectile.SpawnPos.x + aliveTime * ProjectileSpeed;
}}
[WithAll(typeof(Cube))]
[BurstCompile]
public partial struct FallingCubeJob : IJobEntity
{
public float3 Movement;
public EntityCommandBuffer.ParallelWriter ECB;
void Execute([ChunkIndexInQuery] int chunkIndex, Entity entity, ref LocalTransform cubeTransform)
{ cubeTransform.Position += Movement;
if (cubeTransform.Position.y < 0)
{ ECB.DestroyEntity(chunkIndex, entity);
} }}