Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using addressables to offload asset storage locally

Discussion in 'Addressables' started by ina, Oct 20, 2020.

  1. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Are addressables designed to be used to store assets remotely instead of locally?

    Project A1: For example, an artist's local project has the art prefabs assets.

    Project A2: A scripter/programmer's local project does not necessarily need all the art prefabs, except when loaded selectively.

    Does Project A2 need to have the same art prefabs locally, or can Project A2 load the A1 prefabs completely remotely?
     
  2. LucasHehir

    LucasHehir

    Joined:
    May 7, 2020
    Posts:
    74
    Completely remotely, as long as you go about it in the right way.

    If you want to load a specific asset (i.e. reference one particular prefab directly), then you'll need to have that prefab locally in order to point an AssetReference at it.

    However... if you were to use 'asset labels' (which I recommend for this exact reason!) to query Addressables in such a way that you know it will result in your target prefab, you can work without the prefab being local at all. For example, if you give the target prefab a couple of specific asset labels like:
    • "prefab"
    • "player"
    • "android"
    And you were certain that prefab would be the only one to match the label cross-section... you could do the following to fetch it:

    Code (CSharp):
    1. AsyncOperationHandle<IList<IResourceLocation>> locationOp = Addressables.LoadResourceLocationsAsync(new string[]
    2.    {
    3.        "prefab",
    4.        "player",
    5.        "android"
    6.    }, Addressables.MergeMode.Intersection);
    MergeMode.Intersection is the secret sauce there, since it guarantees that you will only receive assets that have all of the given labels. Next, do all your usual defensive clauses/error checking to ensure the operation didn't time-out etc... then:

    Code (CSharp):
    1. var newPlayerInstance = Addressables.LoadAssetAsync(locationOp.Result[0]);
    By doing that (and ticking all the other make-it-work-right boxes), someone in project A2 could rely on being able to instantiate a prefab they don't have locally or ever know about.
     
    FlightOfOne likes this.
  3. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    @LucasHehir
    Could you elaborate on this part please? What exactly is the difference between using an asset reference vs. key, e.g. pros and cons of each?
     
  4. LucasHehir

    LucasHehir

    Joined:
    May 7, 2020
    Posts:
    74
    My current understanding is:

    By key
    You can pass in a key string directly to Addressables.LoadAssetAsync if you know what it is. This is the least convenient way that I know of because it requires you to know and potentially update the key which is prone to error. I straight up don't recommend doing this.

    By asset reference
    These are super convenient, they let you pick one specific asset to load. So if we're talking about the background music file and you know that asset is never going to be dynamic, just use an AssetReference to load/unload it. Done. The only downside here I guess is that they're not really dynamic, so you're stuck with whatever you've put in there. You can create new AssetReferences by passing in a guid but this might be more for fringe use-cases.

    By asset label
    My personal favourite. If you've labelled all your assets nicely (i.e. a tree prefab might have the labels "prefab", "scenery", "exterior" etc) you can simply pass in a List<AssetLabelReference> and a specific MergeMode to hone in on one or more assets that you're after. A clever use might be to request all prefabs with the labels "prefab" and "scenery", and then dynamically tack on a label of whichever platform you're currently on to get platform-specific scenery. This is the most flexible in my opinion. The only downside I know of is that MergeMode could be replaced with a more verbose AND/OR system so you could request, say, everything with the label "prefab" and "city" but not "PS4" or something. MergeMode only has a few options and its possible to work around still.
     
    FlightOfOne likes this.
  5. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Thanks for the detailed explanation!