Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Why are there no synchronously LoadAsset APIs?

Discussion in 'Addressables' started by zhuxianzhi, Jul 11, 2018.

  1. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    15
    Another question, what if the resource does not call Addressables.ReleaseAsset?
     
  2. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    53
    My understanding is that the system needs to work equally well with remote and local assets. If they did a synchronous load on a remote asset, it could be a noticeable performance issue depending on how long it takes to load. I'm not sure why there is no synchronous load for assets that are placed in a local group, though.
     
  3. PaulBurslem

    PaulBurslem

    Unity Technologies

    Joined:
    Oct 7, 2016
    Posts:
    42
    Correct - this was a design decision to allow for a seamless transition when moving your assets from a local location (Resource folder, StreamingAssets, etc) to a remote location without having to change code. The returned IAsyncOperation is yieldable so you can use it in coroutines.
     
    RecursiveFrog and karl_jones like this.
  4. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    15
    Not all projects need to load remote assets, there is no synchronous LoadAsset API, and you can't quickly replace the original asset loading system (Resources API)
     
    nepoez likes this.
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,528
    If you really need the asset to be finished loading before you continue the main thread, I guess you could just do a
    while(!loading.isDone);
    on the IAsyncOperation. That can also be extracted into a helper method pretty easily.
     
    RecursiveFrog likes this.
  6. Kastenfrosch2

    Kastenfrosch2

    Joined:
    Mar 19, 2017
    Posts:
    4
    @Baste
    Code (CSharp):
    1. ResourceManagement.IAsyncOperation<GameObject> loading = Addressables.LoadAsset<GameObject>("Foo");
    2. while (!loading.IsDone) {}
    3.  
    Unfortunately the loop never finishs when being called from the Main Thread.
    I guess resource loading is ticked from within the MainThread itself :(.
     
  7. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    119
  8. Kastenfrosch2

    Kastenfrosch2

    Joined:
    Mar 19, 2017
    Posts:
    4
    @MNNoxMortem The problem with this approach is, that it's not really sync. So I cannot guarantee that after I called my Load() function, I can immediately acces the loaded object in the code line afterwards.
    That takes away some simplicity of the old Resources.Load() approach.
     
    nepoez and MNNoxMortem like this.
  9. jayatubi

    jayatubi

    Joined:
    Dec 9, 2013
    Posts:
    8
    Some third party library need synchronous IO interfaces for example the lua script integration. To implement the lua `require` function the program should return content of the requried script immediately. The lua vm was written in c which is not yieldable thus the asynchronous IO can't work for it. For now we have preload all the lua scripts into memory on the startup.
     
    zyc_dc likes this.
  10. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    24
    Yeah. I also have the problem when using Lua. We really need sync load.
     
  11. gsantosgeneragames

    gsantosgeneragames

    Joined:
    Nov 21, 2018
    Posts:
    6
  12. nepoez

    nepoez

    Joined:
    Sep 11, 2011
    Posts:
    277
    Yes we need sync load. It's a real pain for an existing large project that relies heavily on sync load to convert to the new system :(
     
  13. Kirsche

    Kirsche

    Joined:
    Apr 14, 2015
    Posts:
    27
    Synchronous API is also needed if you want to create GUI programmatically like this:

    Code (CSharp):
    1.  
    2. private void CreateGUI()
    3. {
    4.     Window window = new Window("Options");
    5.     window.SetSize(400, 300);
    6.     window.SetAlignment(HAlign.Left, VAlign.Top);
    7.     window.SetPosition(50, 250);
    8.  
    9.     Panel panel = new Panel(window);
    10.     panel.SetBackgroundColor(255, 0, 0, 155);
    11.     panel.SetStretch(true, true);
    12.  
    13.     TabPanel tabpanel = new TabPanel(panel);
    14.     tabpanel.SetStretch(true, true);
    15.  
    16.     var tab = tabpanel.AddTab("Graphics");
    17.     var list = tab.AddComponent<VerticalLayoutGroup>();
    18.     list.childForceExpandHeight = false;
    19.     list.spacing = 5;
    20.  
    21.     // ...
    22. }
     
  14. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    294
    Is that an editor window? or are you making a GUI window inside your game's runtime?

    Though it's on the list, Addressables currently can't be utilized at edit time.
     
    MNNoxMortem likes this.