Search Unity

Is this a good approach? (Managed component, JobComponentSystem)

Discussion in 'Entity Component System' started by illinar, Dec 19, 2019.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    I've got this code and I feel like some things could have been done differently and perhaps better.

    1) Copying into transform from ForEach vs Reading from entities in MonoBehavior
    2) Using a job system in a system with no jobs, just because normal system doesnt seem to support managed IComponentData
    3) Calling jobHandle.Complete() Because otherwise there is a read/write conflict.

    What could I have done better here?

    Code (CSharp):
    1. [UpdateAfter(typeof(TransformSystemGroup))]
    2. public class CopyToObjectTransformSystem : JobComponentSystem
    3. {
    4.     protected override JobHandle OnUpdate(JobHandle handle)
    5.     {
    6.         handle.Complete();
    7.  
    8.         Entities.ForEach((CopyToObjectTransform transform, in LocalToWorld localToWorld) =>
    9.         {
    10.             transform.TargetTransform.position = localToWorld.Position;
    11.             transform.TargetTransform.rotation = localToWorld.Rotation;
    12.         }).WithoutBurst().Run();
    13.  
    14.         Entities.ForEach((CopyToObjectPosition transform, in LocalToWorld localToWorld) =>
    15.         {
    16.             transform.TargetTransform.position = localToWorld.Position;
    17.         }).WithoutBurst().Run();
    18.  
    19.         return handle;
    20.     }
    21. }
     
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    You can use AlwaysSynchronizeSystemAttribute to make a JobComponentSystem behaviour like a ComponentSystem:

    Code (CSharp):
    1.  
    2. public class CopyTransform : IComponentData {
    3.     public Transform Target;
    4. }
    5.  
    6. [AlwaysSynchronizeSystem, UpdateAfter(typeof(TransformSystemGroup))]
    7. public class CopyTransformSystem : JobComponentSystem {
    8.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    9.         Entities
    10.             .ForEach((CopyTransform transform, in LocalToWorld localToWorld) => {
    11.                 transform.Target.SetPositionAndRotation(localToWorld.Position, localToWorld.Rotation);
    12.             })
    13.             .WithoutBurst()
    14.             .Run();
    15.      
    16.         return default;
    17.     }
    18. }
    19.  
    There is also an old IJobParallelForTransform that can be used to write the transform using jobs. I'm not sure about future support to this.

    []'s
     
    illinar likes this.
  3. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    Nice tips. Thanks.