Search Unity

Loading Assets by Name and Label

Discussion in 'Addressables' started by MGGDev, Dec 4, 2018.

  1. MGGDev

    MGGDev

    Joined:
    Nov 6, 2018
    Posts:
    27
    Hello,

    I want to use different skins in my game. I have multiple objects with Image component attached, and I want to link each object to multiple assets, each one corresponding to a skin.

    So, here is what I did:
    1- Created 2 groups: EvilSkinGroup and GoodSkinGroup.
    2- Added the EvilButtonImage.png to the Evil group, gave it the label "EvilLabel", and named the asset "Button".
    3- Added the GoodButtonImage.png to the Good group, gave it the label "GoodLabel", and named the asset "Button" too.

    Now I want to set a variable in my Manager called "Skin_Label" which can be "EvilLabel" or "GoodLabel".
    Then in the inspector, link each object to the base image it should display, let's assume it is the "Button" from the Good group for now.
    When the user picks a new skin, the Skin_Label will change to reflect their selection, and the object will still use the "Button" image, but now it will pick the "Button" with the label similar to the Skin_Label.

    I couldn't get Addressables.LoadAsset<Sprite>() to take two arguments: the Name ("Button") and the Label. Currently, it either accepts a LabelReference, or an asset string address.
    Also, I couldn't make it work using AssetReference, as (in the inspector) it shows the two "Button" (Good and Evil) as two separate assets.

    In short, I want to reference my asset name in the inspector, but dynamically pick the one corresponding to a specific label.
    In theory, I should be able to create new skins after the game's launch, allow the user to download any of them, and the game graphics change without any changes to the code, only the name of the new Skin_Label.

    Can this be achieved with the current system, and how?

    Thanks a lot.
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    yes, this can be achieved, and is one of the workflows we encourage. The perhaps non-intuitive detail that will give you this feature is to use LoadAssets, not LoadAsset. For the singular case, we restrict the input key. But for the case where you are expecting an IList of results, (the plural), you can provide multiple keys. Just set the MergeMode to MergeMode.Intersection.

    then the first (and only) item in your IList should be your desired button.

    -Bill
     
    batvink and MGGDev like this.