Search Unity

[Help] On demand asset reference

Discussion in 'Scripting' started by joaobsneto, Apr 17, 2019.

  1. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    Hi,

    I'm extensively using Scriptable Objects in my project. I have an Scriptable Object that have references to all characters Prefabs in the game. If any asset in the scene has reference to this Scritpable Object, then Unity load all prefabs in memory, and recursively it loads animations, sprites, etc. and uses a lot of memory.
    How can I have an asset that can make reference to many prefabs without loading them in memory, or loading then just when I need?

    Thanks!
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    You can't late-load prefabs, because they don't exist by the time the game is compiled. You can late-load assets, such as sprites, textures, audio and video. https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html

    You probably shouldn't worry about small things like animations. They're just a few numbers.
     
    joaobsneto likes this.
  3. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    Thanks! But I didn't understand what you mean by "You can't late-load prefabs". I can put prefabs in resources folder and load them by path. I don't like using Resource folder, but I think it's the easiest way.
     
  4. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    The problem with animation is that they have reference to the sprites.
     
  5. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    I mean that prefabs are only a convenience for developers. By the time your project is compiled, they become a list of resources, and code to construct the object. Prefabs don't exist in the compiled code, so there's nothing to late-load. But it looks like AssetBundle might support prefabs anyway (probably as a list of assets). So that's worth looking into. It might help with the animations, if the animations are part of the bundle.

    Maybe a more experienced Unity developer than me can step in here. This has to be a solved problem (or it's a huge omission by the Unity team).
     
    joaobsneto likes this.