Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to Jobfy this ComponentSystem-based code?

Discussion in 'Entity Component System and C# Job system' started by nzhangaudio, Aug 13, 2018 at 8:13 AM.

  1. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    5
    Let's say I have a bunch of source positions that need to be updated to some target positions, and each target's entity information is contained by the source positon's companion EntityHolder component. The only way I can think of is using ComponentDataFromEntity<Position> to take that entity info from that "Position(source) & EntityHolder" ComponentGroup and write to it in a for loop. I wonder how this should be implemented in the job system.

    Code (CSharp):
    1. public class PositioningSystem : ComponentSystem
    2. {
    3.     struct SourceGroup
    4.     {
    5.         public readonly int Length;
    6.         [ReadOnly] public ComponentDataArray<Position> Positions;
    7.         [ReadOnly] public ComponentDataArray<EntityHolder> EntityHolder;
    8.     }
    9.     [Inject] SourceGroup sourceGroup;
    10.  
    11.     [Inject] ComponentDataFromEntity<Position> TargetPosition;
    12.  
    13.     protected override void OnUpdate()
    14.     {
    15.         for (int i = 0; i < sourceGroup.Length; i++)
    16.         {
    17.             TargetPosition[sourceGroup.EntityHolder[i].TargetEntity] = sourceGroup.Positions[i];
    18.         }
    19.     }
    20. }
     
    Last edited: Aug 13, 2018 at 8:25 AM
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    49
    Just derive from JobComponentSystem and launch a Job. There is nothing in your code making this a problem. All Injected arrays are automatically protected. While your Job is running no other System accessing Position or writing to EntityHolder components is allowed to run.
     
  3. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    5
    The problem was I can't put
    [WriteOnly] ComponentDataFromEntity<Position>
    inside a job.