官方示例HelloCube 注释理解:这是一个旋转逻辑的脚本,场景中存在一个挂载LocalTransform, RotationSpeed脚本的对象,控制该对象旋转
public partial struct RotationSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
//等待ExecuteMainThread存在了之后,才会执行OnUpdate,不想Mono一样,只能通过这种方式控制OnUpdate执行的顺序,
//原型:ExecuteMainThread : IComponentData,ExecuteMainThread是一个组件
state.RequireForUpdate<ExecuteMainThread>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
//每帧的时间
float deltaTime = SystemAPI.Time.DeltaTime;
//查找每一个具有RefRW<LocalTransform>, RefRO<RotationSpeed>组件的实体,然后遍历,跟新位置
foreach (var (transform, speed) in
SystemAPI.Query<RefRW<LocalTransform>, RefRO<RotationSpeed>>())
{
transform.ValueRW = transform.ValueRO.RotateY(
speed.ValueRO.RadiansPerSecond * deltaTime);
}
}
}