Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Is it bad practise to load prefab synchronously on Main Thread (even in my case) ?

Discussion in 'Addressables' started by Inward, Nov 25, 2022.

  1. Inward

    Inward

    Joined:
    Jun 27, 2015
    Posts:
    7
    Hi ! Basically my goal is a Pokemon-like game where you travel and encounter random enemies (among a lot of different kind), and trigger a turn-based Battle scene.

    Since the enemies can be "random", I need to make this Battle scene generic so it can load any kind of enemies.

    At first, I thought I would create a GameObject referencing all the enemy prefabs and I check which ones need to be Instantiated when the scene is loaded.
    I found a pretty similar case here : https://forum.unity.com/threads/scene-references-to-prefabs-and-memory.824052/
    And there is a problem with this solution : Unity will load every prefabs in memory in case we need it in the scene.
    With a Pokemon-like game, we know from start which ones we need and it's not useful to load all the others.

    So it seems Addressables is the right choice : I will only have a reference of the resource and I will allocate memory only for those who need it.
    But the good practise seems to be asynchronously-only.


    In my case I'd like to load and instantiate the prefabs during the scene load, so everything is at the right place at the first frame of the scene. That's why I was thinking about loading the Addressables on Awake() synchronously to achieve that.
    Is it bad ? If so, is there a better way to do it and prevent some frames with empty screen ? (I know it will be really fast since my prefabs are locally stored, but I'd like to create the best optimized experience as possible)

    Thanks by advance for your help !
     
  2. Inward

    Inward

    Joined:
    Jun 27, 2015
    Posts:
    7
    I just tested to load a scene with a Thread.Sleep task running on the MainThread in Awake(), it freezes the UI of the others scenes too, making it impossible to have a dynamic UI loading screen

    But according to this documentation page https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/SynchronousAddressables.html
    "It is worth noting that calling WaitForCompletion may have performance implications on your runtime when compared to Resources.Load or Instantiate calls directly. If your AssetBundle is local or has been previously downloaded and cached, these performance hits are likely to be negligible."

    Maybe this means loading a few prefabs with WaitForCompletion synchronously will be negligible
     
    Last edited: Nov 25, 2022
  3. LuGus-Jan

    LuGus-Jan

    Joined:
    Oct 3, 2016
    Posts:
    169
    Using Thread.Sleep stalls the main thread and it won't actually let the resources be processed, since that happens on the main thread as well, even the async one: it needs the resources be synced to the resources pool, which happens on the main thread.

    We use WaitForCompletion in several places in our project, and we've not found any downsides yet (we have all our data local, it of course, has a much larger implication when using remote assets). I read a few topics that benchmark the WaitForCompletion method versus loading from resources, and it's a little bit slower, but in my opinion negligible when doing it during a loading screen. It could cause frame hiccups when using it during active player playtime, and I would definitely recommend using the async method then, but during loading screens, I wouldn't make too big a fuss about it.
     
    Inward likes this.
  4. Inward

    Inward

    Joined:
    Jun 27, 2015
    Posts:
    7
    It seems like we're in the same situation using WaitForCompletion, thank you for your feedback!