Search Unity

Some questions about Addressables.InstantiateAsync method

Discussion in 'Addressables' started by cdytoby, Aug 16, 2019.

  1. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181
    Unity 2018.4.6, Addressable 1.1.7

    1. Addressables.InstantiateAsync() has a parameter called trackHandle, "If true, Addressables will track this request to allow it to be released via the result object.". What is the meaning of "released via result object"? From my understanding, result is an GameObject, and upon instantiate, it's an individual GameObject now. How does Addressable track it? And what if this parameter is false?
    2. I have overall GameObject Recycle mechanism in our project, the instantiated gameobjects will be put under a "DontDestroyOnLoad" GameObject Transform before Scene Load. And this Addressables.InstantiateAsync result gameobject won't be "unloaded" during Scene change, right?
     
    Last edited: Aug 16, 2019
    ritesh_khokhani likes this.
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    var handle = Addressables.InstantiateAsync(key, trackHandle=true/false);

    1. Result object instantiated via Addressables.InstantiateAsync are managed by the system.
    • If trackHandle=true, the handle object returned also get managed (in a result-to-handle dict.) When you call Addressables.ReleaseInstance(resultObject), the stored handle will be released as well.
    • If trackHandle=false, you're responsible for release the handle object.

    2. Result object instantiated via Addressables.InstantiateAsync will be released during scene changing. A lazy-man's memory management feature. But I'm not sure how it behaviour with DontDestroyOnLoad trick, you can test it. A *safe* way would be LoadAssetAsync, then GameObject.Instantiate to get unmanaged object.
     
  3. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181

    Thanks for the reply, I still have the question about `trackHandle`, if trackHandle is false, should I manually release the handle? I mean, I instantiate an GameObject, assume it doesn't depend on any other assets, and itself is a "DontDestroyOnLoad", should I manually release the handle? Because the handle itself is a local variable and it's not managed in my code.
     
  4. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    If trackHandle is false, you manage the handle yourself. And release the handle (instead of release the game object), when you want to destroy the instantiated game object. See details on https://docs.unity3d.com/Packages/com.unity.addressables@1.1/manual/MemoryManagement.html

    If trackHandle is true, the system manages the handle, and it get released when with instantiated game object (via ReleaseInstance call, or changing scene).
     
    Last edited: Aug 19, 2019
    ritesh_khokhani and unity_bill like this.