using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; using UnityEngine;
// This system updates all entities in the scene with both a RotationSpeed_ForEach and Rotation component. publicclassRotationSpeedSystem_ForEach : ComponentSystem { protectedoverridevoidOnUpdate() { // Entities.ForEach processes each set of ComponentData on the main thread. This is not the recommended // method for best performance. However, we start with it here to demonstrate the clearer separation // between ComponentSystem Update (logic) and ComponentData (data). // There is no update logic on the individual ComponentData. Entities.ForEach((ref RotationSpeed_ForEach rotationSpeed, ref Rotation rotation) => { var deltaTime = Time.deltaTime; rotation.Value = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle(math.up(), rotationSpeed.RadiansPerSecond * deltaTime)); }); } }
// The MonoBehaviour data is converted to ComponentData on the entity. // We are specifically transforming from a good editor representation of the data (Represented in degrees) // To a good runtime representation (Represented in radians) publicvoidConvert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { var data = new RotationSpeed_ForEach { RadiansPerSecond = math.radians(DegreesPerSecond) }; dstManager.AddComponentData(entity, data); } }
namespaceSamples.HelloCube_1b { // Serializable attribute is for editor support. [Serializable] publicstruct MoveUp_ForEachWithEntityChanges : IComponentData { // MoveUp is a "tag" component and contains no data. Tag components can be used to mark entities that a system should process. } }
1 2 3 4 5 6 7 8 9 10 11 12 13
using System; using Unity.Entities;
namespaceSamples.HelloCube_1b { // Serializable attribute is for editor support. [Serializable] publicstruct MovingCube_ForEachWithEntityChanges : IComponentData { // MovingCube_ForEachWithEntityChanges is a "tag" component and contains no data. // Tag components can be used to mark entities that a system should process. } }
using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; using UnityEngine;
namespaceSamples.HelloCube_1b { // This system updates all entities in the scene with Translation components. // It treats entities differently depending on whether or not they also have a MoveUp component. publicclassMovementSystem_ForEachWithEntityChanges : ComponentSystem { protectedoverridevoidOnUpdate() { // If a MoveUp component is present, then the system updates the Translation component to move the entity upwards. // Once the entity reaches a predetermined height, the function removes the MoveUp component. Entities.WithAllReadOnly<MovingCube_ForEachWithEntityChanges, MoveUp_ForEachWithEntityChanges>().ForEach( (Entity id, ref Translation translation) => { var deltaTime = Time.deltaTime; translation = new Translation() { Value = new float3(translation.Value.x, translation.Value.y + deltaTime, translation.Value.z) };
// If an entity does not have a MoveUp component (but does have a Translation component), // then the system moves the entity down to its starting point and adds a MoveUp component. Entities.WithAllReadOnly<MovingCube_ForEachWithEntityChanges>().WithNone<MoveUp_ForEachWithEntityChanges>().ForEach( (Entity id, ref Translation translation) => { translation = new Translation() { Value = new float3(translation.Value.x, -10.0f, translation.Value.z) };