Search Unity

AssetReference from AddressableAssetEntry

Discussion in 'Addressables' started by cmorab, Aug 10, 2018.

  1. cmorab

    cmorab

    Joined:
    May 8, 2017
    Posts:
    1
    Hello everyone,

    While we're waiting for the API documentation to get put up I wanted to know if anyone here knew how I could create an AssetReference from an AddressableAssetEntry?

    Thanks in advance!
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    We are planning to add some better interfaces to create AssetReference instances, but for now, you have to create a new AssetReference, then set it's editorAsset. To do that from an AddressableAssetEntry, you most likely would want to ask the entry for it's guid, then use the AssetDatabase to turn that into an object.

    This is not a very clean path, which is why we intend to make it better in the future.

    Thanks for trying out the system,
    Bill
     
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    How would you add an asset to the addressable asset group?

    I have a big FBX(>90mb) that I split up in several smaller FBX's (for more modular spawning as not every part is used). I want to add those to a new addressable asset group via code, so I can process assets automatically instead of having to add them to the list manually.
    After exporting them as FBX with the FBX Exporter, I have a list of paths to the fbx's.
    I then want to convert them all to addressables by the given group name.

    I've tried the piece of code below but, for some reason it only creates the group and not the asset entries.
    So do I need to manually add the entry after it is created?
    The
    CreateGroup
    method created the group and listed it though, but it seems the
    CreateOrMoveEntry
    only creates an instance but does not add it to the group even though the group is specified.

    Code (CSharp):
    1. private void CreateAddressableAssetGroup(List<string> assetList, string groupName)
    2. {
    3.     var addressableSettings = AddressableAssetSettings.GetDefault(false, true);
    4.     if (addressableSettings.IsNotUniqueGroupName(groupName))
    5.         addressableSettings.RemoveGroup(addressableSettings.FindGroup(groupName));
    6.     // Recreate new Group
    7.     var newGroup = addressableSettings.CreateGroup(groupName, typeof(UnityEngine.Object), false, false);
    8.  
    9.     // Add all items to the group
    10.     foreach (var assetPath in assetList)
    11.     {
    12.         var relativeAssetPath = assetPath.Replace(Application.dataPath, "Assets");
    13.         var assetGUID = AssetDatabase.AssetPathToGUID(relativeAssetPath);
    14.         addressableSettings.CreateOrMoveEntry(assetGUID, newGroup);
    15.     }
    16. }
    17.  
    Edit: ah nevermind... I just noticed I didn't use
    AssetDatabase.Refresh
    .
    The asset database wasn't up to date so it couldn't get the GUID and thus not add it to the group.
     
    Last edited: Aug 16, 2018
    Leniaal likes this.
  4. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    glad you figured that out, or I would have been staring at that code with no idea why it wasn't working :)
     
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    yeah it is easy to forget a refresh of the AssetDatabase after creating assets via code :p
     
  6. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    Hi, we're just starting with addressables, we're struggling with the documentation right now as we want to drive everything dynamically via script as we have 1000s of assets..
    The api of course has changed but we're assuming we need to create groups in the AddressableAssetSettings and add in our stuff

    In the old example above

    1. var addressableSettings = AddressableAssetSettings.GetDefault(false, true)

    This doesn't exist anymore, what is the recommended way of getting the AddressableAssetSettings so we can create groups for the latest version?

    thnx

    *Nevermind, we figured it out. this is what we're using, hopefully of some use.

    var test = AddressableAssetSettingsDefaultObject.Settings;
    test.CreateGroup("Test", false, false, false, null, typeof(BundledAssetGroupSchema));
    AssetDatabase.Refresh();
     
    Last edited: Sep 12, 2019