Search Unity

How to change Addressable Group's Build Path from code?

Discussion in 'Addressables' started by Kamyker, Jan 8, 2020.

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    I'm blind or this is over-complicated...

    Edit
    Tried this but it doesn't work:
     
    Last edited: Jan 8, 2020
  2. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Found it, it's impossible to change the "<custom>" profile/option in Build Path but every other can be changed with:

    Code (CSharp):
    1. groupAsset.Settings.profileSettings.SetValue( groupAsset.Settings.activeProfileId, "LocalBuildPath", yourPathHere );
    My Feedback: there should be simple groupAsset.Settings.BuildPath that could be set.
     
  3. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Code (CSharp):
    1. BundledAssetGroupSchema bundledAssetGroupSchema =
    2.                         addressableAssetGroup.GetSchema<BundledAssetGroupSchema>();
    3.                     bundledAssetGroupSchema.Compression = BundledAssetGroupSchema.BundleCompressionMode.LZMA;
    4.                     bundledAssetGroupSchema.BuildPath.SetVariableByName(aaSettings,
    5.                         aaSettings.profileSettings.GetValueById("Default", "RemoteBuildPath"));
    6.                     bundledAssetGroupSchema.LoadPath.SetVariableByName(aaSettings,
    7.                         aaSettings.profileSettings.GetValueById("Default", "RemoteLoadPath"));
    I was able to change Build Path and Load Path in this way in Addressables 1.20.3.
     
    kiohbyun likes this.
  4. Flexford

    Flexford

    Joined:
    Dec 8, 2016
    Posts:
    20
    It's not correct, you set value by name, but in name parameter set conctete value.
    Correct way it's:
    Code (CSharp):
    1. bundledAssetGroupSchema.BuildPath.SetVariableByName(aaSettings, "RemoteBuildPath");
    2. bundledAssetGroupSchema.LoadPath.SetVariableByName(aaSettings, "RemoteLoadPath");
    For set concrete value need use #Kamyker code:
     
    MGhasemi likes this.
  5. fawadasghar82

    fawadasghar82

    Joined:
    Dec 18, 2018
    Posts:
    1
    I am currently facing a similar problem where I need to change the value of the asset provider, but I have been unable to locate a single place where it can be modified programmatically. There is provisioning to access for changing the compression value as well as build mode as mentioned in the above threads but there is no way to set the Asset provider value.

    Code (CSharp):
    1. var settings = AddressableAssetSettingsDefaultObject.Settings.DefaultGroup;
    2.             BundledAssetGroupSchema assetGroupSchema = settings.GetSchema<BundledAssetGroupSchema>();
    3.             assetGroupSchema.Compression = BundledAssetGroupSchema.BundleCompressionMode.LZ4;

    AssetProviderValueChange.png
     
    Last edited: May 23, 2023
  6. timtunity3d

    timtunity3d

    Unity Technologies

    Joined:
    Oct 1, 2015
    Posts:
    131
    @Kamyker these "Custom" fields use a legacy API. "Custom" values are stored as the Id of the variable and there isn't a way to modify them using public APIs.

    What I would suggest is to define a new Path Pair on your Profile. Profiles are setup to have "Local" and "Remote" path pairs by default, but you can add as many as you want.

    To add path pairs I go in and click on Create->Build and Load Path Variables in the Addressables Profiles window:

    upload_2023-6-1_15-7-18.png

    Then I create a new combo called MyTestVars

    upload_2023-6-1_15-8-17.png

    Now I can use code like yours to set my group to my build and load path to this path pair:

    Code (CSharp):
    1.  
    2. var settings = AddressableAssetSettingsDefaultObject.Settings;
    3. var group = settings.FindGroup("groupName");
    4.  
    5. var idInfo = settings.profileSettings.GetProfileDataByName("MyTestVars.BuildPath");
    6. group.GetSchema<BundledAssetGroupSchema>().BuildPath.SetVariableById(settings, idInfo.Id);
    7.  
    8. idInfo = settings.profileSettings.GetProfileDataByName("MyTestVars.LoadPath");
    9. group.GetSchema<BundledAssetGroupSchema>().LoadPath.SetVariableById(settings, idInfo.Id);
    10.  
     
  7. Flexford

    Flexford

    Joined:
    Dec 8, 2016
    Posts:
    20
    Code (CSharp):
    1.  
    2. private static PropertyInfo _bundleProviderProperty;
    3. _bundleProviderProperty = typeof(BundledAssetGroupSchema).GetProperty(nameof(BundledAssetGroupSchema.AssetBundleProviderType),
    4.                                                                                       BindingFlags.Instance | BindingFlags.Public);
    5. public static void SetBundleProvider<T>(BundledAssetGroupSchema schema)
    6. {
    7. _bundleProviderProperty.SetValue(schema, new SerializedType() { Value = typeof(T) });
    8. }
    9.  
     
  8. timtunity3d

    timtunity3d

    Unity Technologies

    Joined:
    Oct 1, 2015
    Posts:
    131
    @fawadasghar82 @Flexford reflection is indeed the only way to set it currently. We plan to make the BundleProvider public in a future release.
     
    Flexford likes this.
  9. won-gyu

    won-gyu

    Joined:
    Mar 23, 2018
    Posts:
    35
    It works but with the "Local.BuildPath" or "Remote.BuildPath" for the recent version of Addressables
     
    npatch likes this.
  10. leutnant_kane

    leutnant_kane

    Joined:
    Apr 29, 2015
    Posts:
    40
    @timtunity3d Sadly we can't use the profile pairs, since the schemas use guids to identify those. Our Addressables are in packages, which means those guids will break when the package is installed in another project. Cloning them into the project via import groups also isn't really viable, since that adds a manual installation/update step for each of our internal asset packages (which frequently get updated in 20+ projects). For this reason we currently use custom build/load paths, but these randomly break, since addressables seems to check for the profile build/load path regardless of set custom paths at several points (one example is the addressableAssetProfileSettings.GetValueById, which is used in ErrorCheckBundleSettings in BuildScriptPackedMode. It gets the custom path as a parameter, but as long as it finds the active profile it will use that for dermining the path - even if that finds nothing and return null.
    So, before I go and start a fork for the package and maintain that, is there a better way?