Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

LazyLoadReference

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

  1. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    23
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,892
    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:
    23
    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:
    151
    Wait!
    Unity can serialize generic classes now?
    Please tell me it does...
     
  5. ddalacu

    ddalacu

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

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,096
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,892
    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:
    735
    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,096
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,892
  11. Prodigga

    Prodigga

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