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

Addressable variants

Discussion in 'Addressables' started by PaulNK, Jul 16, 2018.

  1. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    27
    I had two existing prefabs, both in a bundle called Demo, with the same name “test” and different variants “sd” and “hd”. Unity converted the project into two new groups called “Demo.sd” containing “Assets/sd/test.prefab” and “Demo.hd” containing “Assets/hd/test.prefab”.
    The game might have different combinations of variants e.g quality, skins, season themes.

    I could rename the assets and load them by string names e.g Addressables.Instantiate<GameObject>("Assets/."+variant+"/test"); Then I could set the variant when the game runs to check different quality settings, but then I couldn’t link them in the editor.

    Is it possible to use Addressables.ResourceLocators to replace “sd” with “hd” on the bundles and assets. If it is would this still keep dependencies? I’m guessing this wouldn’t work as the names are probably stored as hashes instead of string names.

    Is there a solution for variants that I’ve missed or are variants not implemented yet?
     
    Deng-Jia likes this.
  2. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    I ended up using scriptable objects and included the themes inside the asset e.g

    Code (CSharp):
    1. public enum Theme {Default, XMas}
    2.  
    3. [System.Serializable]
    4. public class LevelAssetTheme
    5. {
    6.     public Theme theme;
    7.     public AssetReferenceTexture2D previewImage;
    8.     public AssetReference mapScene; // or prefab
    9. }
    10.  
    11. [CreateAssetMenu()]
    12. public class LevelAsset : ScriptableObject {
    13.     public string description; // same for both themes
    14.     public List<LevelAssetTheme> themes;
    15. }

    Then label the LevelAsset instances as “Level”. Because AssetReference is only loaded when requested it shouldn't use much memory. For a level select I just get all instances by label. For the level details screen I’d have a public LevelAsset property. That way I can work on the level details screen without having to go through the level select screen (which would assign the property). The level details screen could then list all themes to pick or just show a global theme e.g XMas based on date.

    Variants will also only support one type at a time e.g character skin variants couldn't show all skins on one screen. There also might not be a theme for all stages e.g only stage 2 has a christmas theme. In this case the theme will revert to the default. The default theme could be local while the christmas theme might be remote.

    If variants are required then I’d just use the regular asset bundles e.g maybe different textmesh pro language fonts (english / chinese characters) in streaming assets.

    On a side note I’m surprised there isn’t more hype about addressables. They solve all the issues I’ve had with asset bundles and resources. I’d even consider it to be more important than nested prefabs.
     
    Stormy102 and PaulNK like this.
  3. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57

    I agree... I had so so much trouble to set my previous project up with asset bundles, as soon as I knew addressables were coming out I wanted to port everything in my current project to use it from the beginning... makes life so much easier to prepare everything from the start to use this kind of approach and gives so much flexibility...
     
  4. arielsan

    arielsan

    Joined:
    Dec 3, 2013
    Posts:
    47
    I've started using Addressables for a new project and even though I am really hyped by it, I still don't know how to solve some issues like variants for different resolution assets (mobile game), mainly for directly referenced assets like sprites inside a UI or even an Animation of that UI, I have a workaround plan of duplicating all prefabs and interfaces or change to load and reference things in runtime but that would be a bit hard to edit since I should have a way to load in editor but only for edition, etc... anxiety...

    I hope to find a good way of dealing with it since I really like Addressables for a lot of features but I am worried about having to move back to asset bundles.
     
  5. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    as a simple example, we created a project that does texture variant creation and usage: https://github.com/Unity-Technologies/Addressables-Sample/tree/master/Advanced/Texture Variations

    It's a bit harder to auto-generate the assets if you are at the prefab level, but the runtime should look the same. So for the case of having an "sd" and "hd" prefab you created manually, you can use labels to make things pretty simple. just give both prefabs the same address, and different labels. See the sample project for reference.
     
    PaulNK likes this.
  6. Seven-Huang

    Seven-Huang

    Joined:
    Nov 28, 2017
    Posts:
    4

    I want to know if the Addressables will support AssetBundle variants or not. Because AssetBundle variants is a very convenient way to load different asset acording different quality without breaking asset references or write much code to slove reference.

    The "Label Solution" you mentioned is not what I want. It can only change asset at "Addressable Level", and hard to change asset at "Dependency Level". For example. I can easily use same prefab and change different model or material or texture with AssetBundle variants and no need to slove reference. And with "Label Solution" I need to create different prefab that reference difference asset or write some code to change model or material or texture in runtime. So I hope Addressables can support AssetBundle variants or if there is a better way please tell me.
     
    ganaware likes this.
  7. hnim

    hnim

    Joined:
    Jul 1, 2015
    Posts:
    11
    i am looking for a way to work with Addressable like AssetBundle variant. With Ab, i can easy unload and load other variant, then all objects will update with new version automatic.
     
  8. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Addressables, under the hood, uses the Scriptable Build Pipeline (SBP). SBP does not build AsestBundle variants. So, out of the box, addressables does not support AssetBundle variants. I think it could be possible to write a custom build script that uses the old built-in pipeline, but that pipeline has a few more issues.
     
  9. aproctor

    aproctor

    Joined:
    Jan 29, 2013
    Posts:
    1
    I was just rewatching the Unite Berlin talk (still the best talk on Adressables IMO), and I was wondering if there was any change in the approach for variants since then.

    I'm considering using tables to drive asset localization in a larger scale project I'm kicking off, and
     
  10. cgascons

    cgascons

    Joined:
    Feb 22, 2016
    Posts:
    25
    I'm quite interested in this topic too. We mainly switched our content management system to Addressables just for the variants, is there any chance these will be supported by Addressables officially at some point @unity_bill ?
     
  11. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    Yeah, concur that it's kind of a mess currently. =|
     
  12. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    The example repo leads to a 404 now. Any update on where to find this @unity_bill ?

    I'm trying to work with parallel remote bundles and would like to be able to load in the correct asset without having to use different addresses, perhaps by pointing at what catalog must provide the asset.
    The idea would be to have variant bundles with the exact same content structure so they would function interchangeably, but still being able that in the case of multiple bundles being loaded I can specify which bundle to use.

    I tried so far with LoadContentCatalogAsync and adding suffixes there, but the suffix doesn't seem to be added anywhere? Or I'm just using things wrong. Documentation is very vague on how the suffix gets used.
     
  13. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    Looks like it was just renamed https://github.com/Unity-Technologies/Addressables-Sample/tree/master/Advanced/Addressable Variants

    Thought I’d just mention what we ended up doing for this. In our case we had high and low resolution UI textures. The build server built the HD asset bundles. Then it resized all the textures and created a new asset bundle for the low resolution asset bundle. The assets are linked by the meta data files so it works similar to the old variant system. I didn’t end up working directly on this part of the system but I think that was how it was set up. I think it did mean that we couldn’t use the crc checksums.
     
  14. DinosCharmGames

    DinosCharmGames

    Joined:
    May 9, 2018
    Posts:
    25
    @CodeKiwi How did you end up doing the linking through meta data files? You can't use the same guid for the HD and SD asset because the will make Unity angry...
     
  15. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    27
    It just swaps the assets between builds. I’m not sure how it’s exactly setup but I think it’s like the following.
    • The editor uses HD images (can be used directly in UI).
    • The build server builds the asset bundles and uploads them to the asset bundle server (HD folder).
    • It then resizes all the images by half, rebuilds the asset bundles and uploads them to the asset bundle server (SD folder).
    • Any changes are discarded after the bundles have been uploaded.
    • The SD assets have the same guids as the HD assets. It just needs to download the correct bundle.
    One issue with this is that you can’t switch between SD and HD assets in the editor.
     
  16. DinosCharmGames

    DinosCharmGames

    Joined:
    May 9, 2018
    Posts:
    25
    Ok so it sounds like this is an option that doesn't use Addressables at all and just falls back on the old asset bundle system? Or are you manipulating the asset bundles underneath the Addressables system?
     
  17. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    The "variants" solution that is in the sample repo is, at best, a very "simple" example of one way to do variants. It's inefficient though and, when implemented 1-for-1 into a project, you will end up with *extremely* long build times -- likely because it's refreshing the asset DB every time it 'generates' an asset variant ("copy").

    In our project, after implementing the 'naive' variants example pretty much directly, it worked as intended -- but it was incredibly slow. IIRC our builds were taking over 8-10 hours to run, might have even been 24hours. Without the variants, our builds run in under 10-20 minutes (that's building against 2 targets /platforms).

    That said, you can probably optimize that -- and, if the content doesn't change much, you should be able to build the variants once & then not have to re-build them with each game build. I'm not sure, we didn't pursue it further.

    One other key takeaway is that the code in the 'variants' example is extremely useful to read/learn from -- if you want to do something such as create "pseudo labels" on-the-fly (which is very helpful & should be built-in IMO), the way to do it is obvious once you read through & understand the variants example!

    An illustrative & very compelling use-case for "pseudo-labels" -- IMO, at least -- is auto-applying a label based on each asset's path/directory. Doing so allows you to do things such as load all assets-of-type that are contained within a folder, which is something you'd kind of expect to already be able to do (especially given that this is the recommended asset resource management [loading/unloading] pipeline & the prior pipeline [Resources.Load] had/has that functionality).

    After setting up "automatic pseudo-labeling" you can do things like this:
    AddressableAssets.LoadByLabel<T>(string label = "DIR_PATH");

    (that's pseudo-code -- I don't recall syntax/method names off-hand)

    That alone makes it worth reading & fully understanding the variants example worth it; but probably don't expect the variants example to "just work" in a production environment without making either substantial adjustments to optimize the variants [unless count of assets w/ variants is very small], or to tailor your workflow to ensure you can build & manage variants as a separate workflow/process from your "main build" workflow/processes.
     
    forestrf and PaulNK like this.
  18. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    27
    It’s still using the addressables system. As far as I know you can’t mix addressables and the old asset bundle system. As another example:
    • Windows standalone project with an addressable background image.
    • Build settings / Build => \StreamingAssets\aa\StandaloneWindows\packedassets_[name][hash].bundle
    • Save this bundle somewhere.
    • Override a sprite e.g. standardBg.png => halloweenBg.png
    • Build settings / Build => new .bundle (will probably have a different hash name)
    • Rename the old bundle to the same name as the new one.
    • You can swap the bundles (might need to disable CRC or add extra code to enable).
    It does increase build times (similar to the variants repo example). It’s probably better to avoid building the variants while testing. This was used in a project that had upgraded to the new addressables system. We would probably try to use a more integrated solution in new projects (maybe based on the variant repo example).
     
  19. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    FWIW, and I'm not an expert (and far less familiar today vs a few months back) and perhaps somewhat pedantic, but...

    My understanding is that Addressables actually is using the "old" AssetBundles system under the hood, and that Addressables is really more of a 'wrapper' that sits on top of the existing AssetBundles system to provide a better developer UI/UX.

    I believe the general theory was that AssetBundles worked fine, but the [dev] UX/UI/workflow was just very poor & confusing. I think there were also a number of "unsolved issues" that each dev-team was commonly having to solve themselves -- I think those issues were mostly related to the management & resolution of asset-dependency chains, and possibly some memory management stuff. Not to mention, it was just a relatively complex workflow and generally required you to build custom tooling around it for anything beyond the most trivial use-cases.

    Take the above with a grain of salt if you'd like -- and/or search through the forum [and the Addressables source] to confirm -- but that's been my understanding (and worth noting, we never used the original/'raw' AssetBundles stuff so my knowledge of it is purely from research/what I've read, nil hands-on experience with it).

    I hope the Addressables developer UI/UX continues to improve. For any smaller projects, we're almost surely going to try to avoid Addressables as long as we can feasibly do so -- instead using a combo of Resources (for lightweight 'config'/data), and custom loading schemes for anything heavier (UnityWebRequest and/or custom threads).

    For any heavier model (and, in some cases, textures), we may have to use Addressables still -- but we'll try to keep it relatively sparing & restricted to only the most egregiously-large assets (specifically those that are difficult to direct-load via custom code, such as models, etc).
     
    PaulNK likes this.