Search Unity

Addressables, ScriptableObjects and DI

Discussion in 'Addressables' started by taylank, Jul 10, 2018.

  1. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    Hi, I just watched the Unite Berlin video and the system looks exciting in general, with some well thought out features. I also noticed most of the examples in the video were about prefab assets, which made me wonder how/if the system works with ScriptableObjects and custom assets.

    In my current project I am using the dependency injection framework Zenject, and I have a bunch of nested SO assets that I use for AI behaviour. When instantiating a certain behaviour, I deep-clone the SO and iterate through the asset's nested members and make sure they are all cloned and have their dependencies injected via Zenject as well, before using it. It works, but it is cumbersome to extend. So my questions are:
    • How does the Addressables system handle nested SOs/assets? If I have a SO that references another SO via an AssetReference instead of its original copy, how does the system handle the instantiation? Are the nested AssetReferences resolved automatically/recursively or would I have to manually do this?
    • The video talked about a Provider interface. Would I be able to use that kind of like a PreProcessor where I can dependency-inject asset instances?
    • How are providers initialized / can they be initialized? Can a Provider implementation be supplied at runtime or do they have to be pre-defined at Edit time?
    • Can we have more examples of Provider interface implementation please?
    Thanks in advance.
     
  2. PaulBurslem

    PaulBurslem

    Unity Technologies

    Joined:
    Oct 7, 2016
    Posts:
    79
    AssetReferences are not loaded until you call LoadAsset or Instantiate. Their purpose is to break the dependency chain that can cause the automatic load of all dependencies. For example if you created a sound bank ScriptableObject with a large array of audio clips, they would all be loaded when the SO is loaded. If the array contained AssetReferences instead, nothing would be loaded until requested and the clips could be local, remote or a mix of the two.
     
  3. PaulBurslem

    PaulBurslem

    Unity Technologies

    Joined:
    Oct 7, 2016
    Posts:
    79
    IResourceProvider is an interface that you can implement and add to the system. A possible implementation for DI would be to create a wrapper provider that does the DI and uses an internal provider to actually load the data.
     
  4. PaulBurslem

    PaulBurslem

    Unity Technologies

    Joined:
    Oct 7, 2016
    Posts:
    79
    The providers can be added at runtime, but the mapping between the location and the provider is based on the class name of the provider. The IResourceLocation.ProviderId is set to typeof(ProviderClass).FullName. For examples of Provider implementation, you can look in Packages/Resource Manager/Runtime/ResourceProviders.