Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

LazyLoadReference

Discussion in '2019.3 Beta' started by ddalacu, Aug 9, 2019.

  1. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    31
    Hello, today i was browsing trough unity s source code and i found LazyLoadReference, now i have a 2 questions:
    1: Will this get backported??
    2: Does this work at runtime?
    Btw the way i do this in older unitys is like:
    Code (CSharp):
    1. public class NoRefObject
    2.     {
    3.         private string _json;
    4.         [SerializeField]
    5.         private Object o;
    6.  
    7.         private int instanceId;
    8.  
    9.         public int ObjectInstanceId => instanceId;
    10.  
    11.         public NoRefObject(Object o)
    12.         {
    13.             if (o == null)
    14.             {
    15.                 throw new ArgumentNullException("No ref object");
    16.             }
    17.  
    18.             this.o = o;
    19.             instanceId = o.GetInstanceID();
    20.             _json = JsonUtility.ToJson(this);
    21.             this.o = null;
    22.         }
    23.         /// <summary>
    24.         /// Deserializes the internal json and returns the AssetReference
    25.         /// </summary>
    26.         public Object GetInstance()
    27.         {
    28.             JsonUtility.FromJsonOverwrite(_json, this);
    29.             Object ret = o;
    30.             o = null;
    31.             return ret;
    32.         }
    33.     }
    but this eats memory :(, i really hope this gets into older versions of unity
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Not to 2018.4, unless they break their rules:
    https://unity3d.com/unity/qa/lts-releases

    If runtime refers to a build/player, then it seems the LazyLoadReference does stay intact, but will actually not be loaded lazily:
    https://docs.unity3d.com/2019.3/Documentation/ScriptReference/LazyLoadReference_1.html

    "loaded when the player is loaded" sound vague to me though. Is it done at player startup? Do they refer to assets Resources? They probably don't resolve all LazyLoadReference's that exist in all scenes when the player is loaded, if these scenes are not loaded yet.
     
  3. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    31
    Yes when i was asking about runtime i was refering to builds lazy loading, would be a pitty if this would not work :(
     
  4. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    438
    Wait!
    Unity can serialize generic classes now?
    Please tell me it does...
     
  5. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    31
    No, sadly it does not suport generic serialization.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You can look at the docs here, and the source here. The big comment block up top seems to indicate that this is made for speeding up editor work, by allowing you to open a scene without every single thing referenced in that scene being immediately loaded.
     
    Peter77 likes this.
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I wonder why they didn't implement it as an attribute in this case. This would then allow us to just decorate a field with said attribute and everything else in user code stays the same. It would then be trivial to even change existing code to use the attribute.

    Right now, with this LazyLoad struct, not being able to work with the reference directly because of this one level of indirection, only for an editor side improvement, seems odd to me. Not a fan of adding indirection.
     
  8. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Isn't this useful for runtime, too? Maybe I am misunderstanding.

    Consider a simple example where a scriptable object 'defines' a character in a game like CrossyRoads. It contains the name of the character (String), an Icon for the character (Texture2D) and its in game prefab (GameObject). You could reference the Icon and the Prefab using LazyLoadReference, and it would mean the texture and the gameobject (and the meshes, SFX, textures, etc references by that gameobject) aren't loaded into memory when you reference the scriptable object that defines your character.

    That would mean you'd want to make sure it doesn't 'automagically' load the referenced assets unless you specifically request it (hence the need for one level of indirection?) :)
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    That would be useful, but as the docs say:
    Code (csharp):
    1. - In a standalone player, all assets are loaded when the player is loaded, or when asset bundles are loaded.
    If you need something like this for runtime, you'd go with Addressables, which provide async loading of assets.
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
  11. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Interesting, then yeah I agree with Peter, the indirection it introduces just for the sake of an editor specific improvement is a little gross.
     
  12. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    41
    Correct, LazyLoadReference<> is relevant only for editor work, in standalone mode its behavior is the same as if it was not present. Why it's not editor only is just to ease it's use (not having to if def stuff)

    Why not an attribute? because the usage is not that straight forward. to get the object you need to explicetly request the object from the LazyLoadReference<> field, an attribute would not have allowed that.

    check out the doc's example: https://docs.unity3d.com/2019.3/Documentation/ScriptReference/LazyLoadReference_1.html
     
  13. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    Will there every be any lazy loading references for runtime?

    Would be so very useful for memory management I cant even begin to explain how much I could use that right now.
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    That's covered by Addressables, isn't it?
     
  15. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    41
    Sorry to say, but at the moment, no work is planned to extend this to the runtime.
    If we see a high demand for this feature, then it might be considered.

    oh, and one last thing I forgot to mention: feature will not be back ported.
     
  16. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    41
    Correct.
     
  17. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    Does addressables actually let you lazy load? I thought it was just a better way at managing loading and references to assets?
    Althought I havent looked into it that much I will admit!
     
  18. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    One of the biggest complaints is that it doesn't have a synchronous load option. So yes, it very much lets you ;)
     
    MadeFromPolygons likes this.
  19. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    77
    Hi,
    Does this API work in the Unity2021 LTS versions of mobile Runtime players?
     
  20. ByMedion

    ByMedion

    Joined:
    May 10, 2018
    Posts:
    19
    Hi! A bit late but it looks like it works on Android.
     
  21. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    77
    Thanks for the reply, actually it does not work on Android.
    Not lazy loading at all on android, referenced asset loaded immediately.
     
  22. BrianJiang

    BrianJiang

    Joined:
    Jun 11, 2017
    Posts:
    7
  23. JamesRebello007

    JamesRebello007

    Joined:
    Dec 21, 2022
    Posts:
    1
    Hi, your plugin does not have anything in the demo scene on how to do initialization. Could you please update that?