Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

LoadAllAssetsAtPath

Discussion in 'Scripting' started by SpongeBob, Apr 20, 2009.

  1. SpongeBob

    SpongeBob

    Joined:
    Feb 13, 2009
    Posts:
    28
    How does this function work ?

    I have 2 textures (.psd files) in my folder and if I do:
    contents = AssetDatabase.LoadAllAssetsAtPath("Assets/female/bundle");
    Debug.Log("contents size: " + contents.Length);

    It says that I have 0 assets there
     
  2. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    Sorry if this is spectacularly unhelpful, but have you tried:

    Code (csharp):
    1. contents = AssetDatabase.LoadAllAssetsAtPath("Assets/female/bundle/");
    (That is, with a "/" at the end of the path)
     
  3. SpongeBob

    SpongeBob

    Joined:
    Feb 13, 2009
    Posts:
    28
    yes, didn't do anything. Thx anyway
     
  4. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    309
    Did you ever solve this? I ran into something similar today, and solved it by using the whole file path (I too thought it would tell me how many FILES were in that path). It seems it will tell you all the assets WITHIN a file.
     
  5. Fehr

    Fehr

    Joined:
    Mar 3, 2011
    Posts:
    23
    Hey Guys,

    The issue here is that LoadAllAssetsAtPath, actually loads all the assets from within a particular file, for instance a .ma binary consists of materials and meshes, and calling AssetDatabase.LoadAllAssetsAtPath("Assets/whatever.mb") would return values, where simply calling it on a path (as its name misleadingly suggests) will not return anything.

    I instead used this solution: http://forum.unity3d.com/threads/18898-How-to-get-list-of-assets-at-asset-path, and it worked a charm..

    The documentation is quite unclear, I hope this clarifies for anyone else wondering why no assets are returned.
     
    GrantTheAnt likes this.
  6. m4a44

    m4a44

    Joined:
    Mar 13, 2013
    Posts:
    41
    Code (CSharp):
    1. foreach (string s in AssetDatabase.FindAssets("", new string[]{"Assets/[your path to objects here]"}))
    2.         {
    3.             [List<Object>].Add(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(s), typeof(Object)));
    4.         }
    Here is what I used. It will get you whatever is in the folder...
     
  7. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    Here's exactly the same as above but as a handy static.
    Code (CSharp):
    1.  public static Object[] LoadAllAssetsAtPath(string path)
    2. {
    3.         if(path.EndsWith("/"))
    4.         {
    5.             path = path.TrimEnd('/');
    6.         }
    7.         string[] GUIDs = AssetDatabase.FindAssets("", new string[] {path});
    8.         Object[] objectList = new Object[GUIDs.Length];
    9.         for (int index = 0; index < GUIDs.Length; index++)
    10.         {
    11.             string guid = GUIDs[index];
    12.             string assetPath = AssetDatabase.GUIDToAssetPath(guid);
    13.             Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object)) as Object;
    14.             objectList[index] = asset;
    15.         }
    16.  
    17.         return objectList;
    18. }
    19.  
    and this one is handy for getting all the types of things throughout the project, regardless of location:

    Code (CSharp):
    1.     public static T[] LoadAllAssetsOfType<T>() where T : Object
    2. {
    3.         string[] GUIDs = AssetDatabase.FindAssets("t:"+typeof(T).ToString());
    4.         T[] objectList = new T[GUIDs.Length];
    5.  
    6.         for (int index = 0; index < GUIDs.Length; index++)
    7.         {
    8.             string guid = GUIDs[index];
    9.             string assetPath = AssetDatabase.GUIDToAssetPath(guid);
    10.             T asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;
    11.             objectList[index] = asset;
    12.         }
    13.  
    14.         return objectList;
    15. }

    Example uses (from editor code, typically).(here, CharacterSO is a SerializedObject)
    Get all objects from a particular path:
    Code (CSharp):
    1.  
    2. CharacterSO[] characterSos = LoadAllAssetsAtPath("Assets/Components/GamePlayData/Characters/") as CharacterSO[];
    3.  
    Get all of a type
    Code (CSharp):
    1. CharacterSO[] characterSos = LoadAllAssetsOfType <CharacterSO>();



    And finally, one that combines the two above (optional path - when no path is passed in, it searches your entire asset database)

    Code (CSharp):
    1.  public static T[] LoadAllAssetsOfType<T>(string optionalPath = "") where T : Object
    2. {
    3.         string[] GUIDs;
    4.         if(optionalPath != "")
    5.         {
    6.             if(optionalPath.EndsWith("/"))
    7.             {
    8.                 optionalPath = optionalPath.TrimEnd('/');
    9.             }
    10.             GUIDs =AssetDatabase.FindAssets("t:" + typeof (T).ToString(),new string[] { optionalPath });
    11.         }
    12.         else
    13.         {
    14.             GUIDs =AssetDatabase.FindAssets("t:" + typeof (T).ToString());
    15.         }
    16.         T[] objectList = new T[GUIDs.Length];
    17.  
    18.         for (int index = 0; index < GUIDs.Length; index++)
    19.         {
    20.             string guid = GUIDs[index];
    21.             string assetPath = AssetDatabase.GUIDToAssetPath(guid);
    22.             T asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;
    23.             objectList[index] = asset;
    24.         }
    25.  
    26.         return objectList;
    27. }

    [Edit] I hit a stumper with this when trying to load textures from a folder. The above works totally fine for singular assets (like your own scriptable objects) but falls down when it's an asset with sub components. I think (i could be wrong) that one must go through every file in the directory and load it direct with LoadAllAssetsAtPath (note "all" referring to the fact that it's a compount asset). What a rigmarole!
     
    Last edited: Mar 9, 2015