Search Unity

How to find assets by substring of name from Addressables?

Discussion in 'Editor & General Support' started by nicmarxp, Feb 20, 2020.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    I have a bunch of textures and I want to load a random one, containing a specific string, for example "hair_".

    I started loading by strings using Resources.Load, but figured I better learn addressable instead for memory, performance and deployment benefits. So I added my textures to an addressables group.

    Then I used the code below, and I got a list of all guids and paths. However each asset turned up twice for each asset. Why are there duplicates?

    Then I also realized AssetDatabase is only for the editor, after building the second time, and I got "The name 'AssetDatabase' does not exist in the current context". Don't know why it worked the first time.

    So I'm pretty confused. How should I use addressables, to find a random texture containing the name "hair_"?

    Also as a bonus question, how can I get a list in a dropdown in the inspector with all adressables matching hair, so I can pick one?

    Here's the code I used so far:

    Code (CSharp):
    1. var textures = AssetDatabase.FindAssets("hair_");
    2. Debug.Log(textures.Length); // This is 14, but I have only 7 assets
    3. foreach (var guid in textures) {
    4.     var path = AssetDatabase.GUIDToAssetPath(guid);
    5.     Debug.Log(guid + " " + path); // The same guid was twice in the list
    6. };
    Thank you in advance! :)
     
  2. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    I watched the Unite talk on adressables for perfomance, and I would guess the reason for the duplicates could be that it converted my Resources folder into an an addressable group, and the same guids is that it detected that it’s the same asset.

    However, I know that you can load assets by label, but I don’t want to manually label each texture, I just want to add a bunch of hair_* textures, and it should automatically find them when I need a hair. So I can just add all textures to the group, and it will find it by code.

    So the questions remains:
    1) How do I find an addressable by wildcard/find like I could with AssetDatabase.Findassets(“hair_”)
    2) Is there any function I can use for the inspector to get a list of addressables, and have them in a dropdown? If I just could get the code in question 1, I could probably do it with Odin Inspector that I’m using.

    Looking forward to your expertise, I’ve really googled myself crazy on this :)
     
  3. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Either addressables isn't used by a lot of people, or people don't have solutions to my problems, but here's some pseudo-code how I solved it. Hope it helps someone.. :)

    The address to the addressable doesn't matter in this case, it just looks at the name of the texture, which in this case is hair_classic01_color.

    Code (CSharp):
    1. Addressables.LoadAssetsAsync<Texture2D>("name_of_addressables_label", null).Completed += objects => {
    2.   foreach (var t in objects.Result) {
    3.     var parts = t.name.Split(new []{"_"}, 5, StringSplitOptions.RemoveEmptyEntries); // 5 = Max parts of the array
    4.     var category = parts[0]; // hair
    5.     var collection = parts[1]; // classic01
    6.     var type = parts[parts.Length - 1]; // color
    7.     // Then store all that match a specific category in a new array, I chose to use a Dictionary.
    8.     // private static Dictionary<string, List<string>> customizationOptionsFiltered = new Dictionary<string, List<string>>();
    9.   }
    10. };
    Let me know if you have any questions, or follow me on Twitter @bluegoogames to see what the heck I'm doing with all this :D
     
  4. sbethge

    sbethge

    Joined:
    Mar 20, 2019
    Posts:
    16
  5. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Oh, thanks for the tip! The doc page is kinda confusing, but I trust you :D