Search Unity

Use of Jobs (basics)

Discussion in 'Entity Component System' started by Vinayak-VC, Jul 29, 2019.

  1. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    Hi there,
    I have created this thread to understand about Jobs System in unity.

    I have looked up to this VIDEO. And its been great about Jobs and shows significant performance boost as if we use Jobs.

    So some weeks ago I was trying to load Image(with 8K resolution) to an Image component. And it was freezing the whole application. There was no workaround.

    But recently I have looked up to this and it seems interesting.
    So can anyone help me with how to download and apply the texture to an image?

    here is the code that I used to do

    Code (CSharp):
    1. IEnumerator LoadFromWeb(string url)
    2.     {
    3.         UnityWebRequest wr = new UnityWebRequest(url);
    4.         DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
    5.         wr.downloadHandler = texDl;
    6.         yield return wr.SendWebRequest();
    7.         if (!(wr.isNetworkError || wr.isHttpError))
    8.         {
    9.             Texture2D t = texDl.texture;
    10.             Sprite s = Sprite.Create(t, new Rect(0, 0, t.width, t.height),
    11.                                      Vector2.zero, 1f);
    12.             image.sprite = s;
    13.             image.enabled = true;
    14.         }
    15.     }


    in this particular code, the freeze occurs on

    Code (CSharp):
    1. Texture2D t = texDl.texture;
    this line.



    *How to call IEnumerator in Jobs.
    *If the string is created in jobs then how to assign it.?
    *How to assign the texture to image from jobs?
     
  2. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    Even though the job system gets you great performance, the job itself is typically only capable of math related functionality. Jobs are basically for number crunching. Stuff like strings and textures are references types (classes) instead of value types (structs). Value types are the only types of variables allowed to be passed to and from a job.

    You can use normal c# threads but the issue there is that most of the object types from Unity aren't allowed to be accessed from other threads at all let alone the job system.

    To answer your question about coroutines, you can't yield inside of a job. A job is designed to start and finish within a single frame.You can create and complete jobs inside coroutines though.

    In short, I don't think there's a work around for what you're trying to do. At least one as simple as just throwing the work into a separate thread.
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You summed it up pretty well except that this is actually not true. There's no limitation on how many frames a job can run for, just that the ECS dependency system will ensure a systems jobs are completed before the system runs again (which is going to be 1 frame.) If you schedule jobs yourself outside of this though you can let them run for multiple frames.
     
  4. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    Thanks for clearing that up @tertle!