Search Unity

Feedback Are there any convenient ways or API to get the reference count of a addressables asset?

Discussion in 'Addressables' started by MarsZhouCN, Jun 20, 2019.

  1. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    Hey guys.
    Are there any convenient ways or API to get the reference count of a addressables asset?
    As in asset bundle, I may keep the reference count of prefab and other asset by myself. But in addressables, did it have a ready-made API for me to use in runtime?

    I used Addressables.ResourceManager.RegisterDiagnosticCallback to do this. But it seems not working when call destroy on a gameObject, only Addressables.ReleaseInstance is usefull to Addressables.InstantiateAsync gameObjectes.​
    Thanks.
     
    Last edited: Jun 21, 2019
    wlwl2 and RecursiveFrog like this.
  2. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    I used Addressables.ResourceManager.RegisterDiagnosticCallback to do this. But it seems not working when call destroy on a gameObject, only Addressables.ReleaseInstance is usefull to Addressables.InstantiateAsync gameObjectes.
     
  3. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    We could probably add this, but I think it might not be a super efficient API. What exactly do you need it for? Are you just wanting a catchall at the end of some section to say "if anything's still ref'd release it"?
     
    wlwl2 likes this.
  4. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    In sometimes I just want to release those no reference assets include prefabs and textures etc.
     
    Last edited: Jul 21, 2019
  5. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    My recommendation is that you keep up with things yourself. If you do need something like this, then the easiest thing is to probably just extend addressables. Keep in mind that it's a package you have full source code access to. Ideally you don't need it, but as of now, I don't think we provide that interface you want.
     
    wlwl2 and Most_Incredible like this.
  6. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    Yes, but i am not dying for modifing the soures code , as i want to keep it same with yours for updating etc.
    I am managing it myself now, Could you please give me some suggestions to realize this?
    Thanks.
     
  7. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Let me clarify what I meant. For what you need, you should be able to extend (not modify) the package. And I think for your needs, having the ability to read/browse our source code can help you with what you need, but you shouldn't need to edit.

    Now, let me ask a clarifying question of your need.
    I may have misunderstood what you are wanting. Which situation are you in:
    1. You have loaded things through addressables, the addressables ref count is above 0, but at some point you want to tell addressables to release everything it has (maybe you are leaving a level or something).
    2. You have loaded things through addressables, and released them, and the addressables ref count is 0. But, since sometimes things stay in memory, you want to force-unload them from memory.
    I had been thinking you were in situation number 1. You wanted a "clean out all the things addressables is referencing". But now after a reread, I'm wondering if you are in situation number 2. Please clarify.
     
  8. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    I load assets with addressables, but i destroy it using GameObject.Destroy , not using Release ,and the ref count in addressables will not be right.
     
  9. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    well, I think we've identified the problem! Just don't do that.

    If you really don't want to keep up with which things are addressable, just write a little helper method like this:

    Code (CSharp):
    1.     static void MyDestroyMethod(GameObject someGameObject)
    2.     {
    3.         if(!Addressables.ReleaseInstance(someGameObject))
    4.             GameObject.Destroy(someGameObject);
    5.     }
    If you absolutely refuse to use Addressables.ReleaseInstance then I'm not sure how I can help you.
     
    wlwl2 and AlkisFortuneFish like this.
  10. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    We don't want to change user's behavior and ask them not to use as this...
     
  11. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    are you an asset store tool?

    I'm somewhat confused by who the users are you don't want to change. It sounds like you're asking someone to change how they load/instantiate (from the normal Unity way, to calling into your system), but you don't want them to change how they unload.

    As a tool creators ourselves, we have decided if you are using our system to load, you need to use our system to unload.

    If you are a tool creator that wants to find a way around that, I'm don't really have any helpful ideas I can think of.
     
    aurelien-morel-ubiant likes this.
  12. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    I am just a common game developer , not a asset store tool.
    I can change other team members in out team's behavior to use our load method as it is normally encapsulated , we do not need to change it no matter we use what type of load system.
    BUT, I do not want to ask those guys not to use unity's Instantiate/Destroy method.
     
  13. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    In such case, you can try to avoid using AddressReference and Addressables.InstantiateAsync, which manage reference counting for you. Just LoadAssetAsync(addressName), cache it somewhere, and use normal GameObject.Instantiate and GameObject.Destroy.
     
  14. MarsZhouCN

    MarsZhouCN

    Joined:
    Dec 19, 2017
    Posts:
    31
    yes, I can managa the references myself , that 's the last way for me . and I was hoping addressables can do it automaticly...
     
  15. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Well it's a technique difficult that Unity does not have a global event processing feature for object destroy. So if the AAS manages reference counting for you, you have to tell the system exclusively when object get destroyed. That probably one of the drawbacks to implement the system as a package, it's not part of the core engine, it can not do too magical things for you.
     
    unity_bill likes this.