Why isn't LoadAllAssetsAtPath returning anything?

I’m trying to write a function in my custom editor to return a list of every asset in the same folder as the object being edited, all objects are contained within subdirectories of the Resources folder. I thought it would be as simple as getting the asset path of the current object, splitting/rejoining that path to remove the asset’s name, then grabbing everything in the same directory:

        string currentPath = AssetDatabase.GetAssetPath(target); //Get the filepath for the asset we're editing

        string[] splitPath = currentPath.Split('/'); //Split and rejoin filepath, excluding the final index so we have a folder path without the filename
        string folderPath = null;
        for (int i = 0; i < splitPath.Length - 1; i++)
        {
            if (i > 0)
            {
                folderPath += "/";
            }
            folderPath += splitPath[i];
        }
        folderPath += "/";


        Object[] objectsInFolder = AssetDatabase.LoadAllAssetsAtPath(folderPath); //Get everything inside that folder
        for (int i = 0; i < objectsInFolder.Length; i++)
        {
            Debug.Log(objectsInFolder[i].name);
        }

When this runs, not only does it not locate the additional .assets contained in folderPath, it doesn’t even return my target.asset. I verified that folderPath is well-formed (in this case, it reads “Assets/Resources/Quests/”), is there something obvious I’m missing or does LoadAllAssetsAtPath simply not work the way I think it does?

You probably want Resources.LoadAll instead of AssetDatabase.LoadAllAssetsAtPath. It is my understanding that the later is used by supplying it with a single object at that path, and it will return all child objects to that object, and that it doesn’t allow for returning all objects stored in a folder. The documentation for LoadAllAssetsAtPath is obviously terrible at explaining this.

1 Like

Ooh no wonder it was acting weird… thank you! I also found that with Resources.LoadAll, it expects a filepath to start after Resources/, and won’t accept anything containing the complete assets/resources/etc path. If it helps anyone, this works just fine for me:

        string currentPath = AssetDatabase.GetAssetPath(target); //Get the filepath for the asset we're editing
        string directoryPath = Path.GetDirectoryName(currentPath);
      
        string[] splitPath = directoryPath.Split('/'); //Split and rejoin filepath, excluding the final index so we have a folder path without the filename
        string folderPath = null;
        bool foundResources = false;
        for (int i = 0; i < splitPath.Length; i++)
        {
            if (foundResources)
            {
                folderPath += splitPath[i] + "/";
            }

            if (splitPath[i] == "Resources")
            {
                foundResources = true;
            }          
        }

I also found this solution for finding Assets by folder path (but not Resources):

private void LoadObjectsFromPath()
{
    string[] obj_guids = AssetDatabase.FindAssets("", new string[] { "[YOUR_PATH]" });

    foreach (string propGuid in obj_guids)
        Debug.Log(propGuid + " --> " + GetObjFromGUID(propGuid));
}

private Object GetObjFromGUID(string guid)
{
    string assetPath = AssetDatabase.GUIDToAssetPath(guid);
    return AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));
}
1 Like