Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

LazyLoadReference

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

  1. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    25
    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:
    4,785
    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:
    25
    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:
    259
    Wait!
    Unity can serialize generic classes now?
    Please tell me it does...
     
  5. ddalacu

    ddalacu

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

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,769
    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:
    4,785
    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:
    841
    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:
    4,769
    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:
    4,785
  11. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    841
    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. GameDevCouple

    GameDevCouple

    Joined:
    Oct 5, 2013
    Posts:
    2,449
    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:
    4,769
    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. GameDevCouple

    GameDevCouple

    Joined:
    Oct 5, 2013
    Posts:
    2,449
    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:
    4,769
    One of the biggest complaints is that it doesn't have a synchronous load option. So yes, it very much lets you ;)
     
    GameDevCouple likes this.
unityunity