Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Job system workflow questions

Discussion in 'C# Job System' started by BakeMyCake, Jan 17, 2019.

  1. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    Can anyone explain to me how to correctly use the job system on a specific simple example:
    Code (CSharp):
    1. void Update()
    2. {
    3.    Vector3 position = transform.position;
    4.    if((position - Toolbox.PlayerTransform.position).sqrMagnitude <= Toolbox.CollisionDistanceSqr)
    5.    {
    6.       Toolbox.PlayerTakeDamage(1);
    7.       m_Renderer.enabled = false;
    8.       m_Particle.Play();
    9.       enabled = false;
    10.   }
    11. }
    I've learned the hard way that with Unity you can get something to work only to later discover you've been doing it completely wrong the whole time.

    What this function does should be fairly self-explanatory, but I'm not sure how to utilize jobs here. I can't disable components or activate particle systems inside a job, correct? Can external static functions be called inside a job? How about accessing static variables? In the example above I'm accessing a static variable to test distance and calling a static function as well, both aren't allowed for jobs right?

    If I understand correctly the only thing parallel jobs can help me with(in this example) is forming a large list of objects that have passed the distance test against the player. I will then need to act out all the object-manipulating based on the results of the list. Is this statement true, or can something else be done with jobs here?

    This example aside, can mesh batching using Mesh.CombineMeshes be done in a job? Can I make use of jobs for physics(without writing my own physics system)?

    It seems to me that jobs have a fairly limited application in real game projects because they are for calculations only. The parallel computation benefits are greatly reduced by the fact that you will need to do "housekeeping" in the main thread after the jobs have finished, and most of the time you will still iterate the list of active objects - the thing you wanted to avoid by using jobs in the first place.
     
  2. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    That's right but it also what make jobs perfect for calculations as long as they have reference/info of [readonly] objects you want them to deal with.

    For your example, there is nothing to throw in jobs since all you do is simple check distance then apply result.

    Check Job System cookbook
    https://github.com/stella3d/job-sys.../Assets/Scripts/MeshVerticesParallelUpdate.cs

    The thing helped you here is how you struct your data and gameobject. Using Jobs force you to think in the way that help you write better performance code.
    And doing stuff in Jobs system help reduce workload on main thread a little bit but the huge boost come from when you seperate struct data from MonoBehavior GameObject. It is like do pooling object by default except this was pooling with data and Jobs.
     
    BakeMyCake likes this.
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Your code can be jobified
    - correct, structural changes should be applied after job (via buffer) on main thread
    - correct, no static in job, you would pass this variable into the job

    I think this from @tertle is a very good resource for you - (in your case IJobProcessComponentData)
    https://www.bovinelabs.com/ecs-hello-world/