Search Unity

How to use the job system to load 1000 textures width & height = 64x64?

Discussion in 'C# Job System' started by WinterboltGames, Apr 20, 2018.

  1. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    Hello Community,

    I have a script that loads 1000 textures from an external folder...

    Here's my code...

    Code (CSharp):
    1. public void LoadTextures()
    2. {
    3.     var images = new DirectoryInfo("Data/Icons").GetFiles();
    4.  
    5.     if (images.Length == 0)
    6.     {
    7.         return;
    8.     }
    9.  
    10.     for (int i = 0; i < images.Length; i++)
    11.     {
    12.         var currentImage = images[i];
    13.  
    14.         var currentImageExtension = Path.GetExtension(currentImage.FullName);
    15.  
    16.         var isPNG = string.Compare(currentImageExtension, "png") == 0;
    17.  
    18.         var isJPG = string.Compare(currentImageExtension, "jpg") == 0;
    19.  
    20.         if (isPNG || isJPG)
    21.         {
    22.             var currentImageBytes = File.ReadAllBytes(currentImage.FullName);
    23.  
    24.             var texture = new Texture(4, 4);
    25.  
    26.             if (texture.LoadImage(currentImageBytes, true))
    27.             {
    28.                 var currentImageName = Path.GetFileNameWithoutExtension(currentImage.FullName);
    29.  
    30.                 icons.Add(currentImageName, texture);
    31.             }
    32.         }
    33.     }
    34. }
    Now the previous code loads images after 30ms everytime the function is called and I have made this function available for modders at runtime to call.

    The question is how can I convert the previous code to Job system code?

    Keep in mind! I have never attempted to use the job system before.

    Thanks!
     
  2. zrrz

    zrrz

    Joined:
    Nov 14, 2012
    Posts:
    40
    I don't think this is possible currently. You're allocating new memory and creating references to that memory, which is currently not how the Job System is supposed to work.

    They might eventually have a solution for I/O, which is a very common multithreaded task, but for now we just have to do I/O on a separate thread or the main thread.
     
    WinterboltGames likes this.
  3. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    What @zrrz said, although I can recommend https://github.com/neuecc/UniRx for this sort of tasks if you can't use ECS/Jobs yet, you can process everything but the final Texture creation on a background thread, and then easily switch back to main thread to create the texture from the byte array.
     
    WinterboltGames likes this.
  4. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    I took a look at UniRx and found while reading on it's page something called MircoCoroutines it seemed powerful and they might help me achieve what I want.

    I hope they do it soon because I really don't like using plugins since I always rely on what's built in unity.
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Just use the normal idiomatic .NET async approaches for something like this. Jobs are designed to solve a different type of problem really.

    Ie just something like Task.Run along with the async file IO api's.
     
  6. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    If you have an example on loading textures in an async function, that would be great! Because I always try to avoid this complex stuff to make the code easily maintainable.
     
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    https://www.dotnetperls.com/async

    Read that to get the basics of async, then basically await a task that does your loading. The loading code doesnt really change, just the way you wrap/call that method + push and pull data from it
     
    WinterboltGames likes this.
  8. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    Thanks for the help sir!
     
    MadeFromPolygons likes this.