- Home /
AssetDatabase.GetSubFolders(path) how to use?
I've been trying to figure out how to use the AssetDatabase.GetSubFolders();
function works with no luck.
The first thing I'm trying is to get all the subfolders of the Assets folder like this: string[] folders = AssetDatabase.GetSubFolders("Assets");
but with no luck.
I get an array of size zero and I can't for the life of me figure out how to use this function. I've attempted googling but to no avail. Can anyone give me some directions here? Thanks!
Having same problem. When I figure it out I'll post here.
Doesn't seem to work for me either. Doesn't seem to work with absolute or relative paths. Any of the following lines will return a zero sized string array:
string[] subs = AssetDatabase.GetSubFolders("C:/");
string[] subs = AssetDatabase.GetSubFolders(Application.dataPath);
string[] subs = AssetDatabase.getSubFolders("Assets");
string[] subs = AssetDatabase.getSubFolders(".");
string[] subs = AssetDatabase.getSubFolders("..");
Answer by Bunny83 · Jun 08, 2015 at 03:58 AM
Like you can read in the documentation of AssetDatabase.GetSubFolders that you have to pass a relative path
an absolute path and not a relative path. GetSubFolders seems to just wrap the
System.IO functions to get the folder information.
edit: Update
Since Unity 2017.1.3 GetSubFolders now requires a relative path, relative to the project folder. So an asset path has to start with Assets/
.
Also keep in mind that the AssetDatabase class is an editor class and can't be used at runtime. So you can only use them in editor script and not in MonoBehaviours.
"How to use it" is perfectly explained in the docs. If you have further problems you should tell us "for what purpose" you want to use that method and include your not working code...
Thanks for the answer! I guess I should've formulated my question better. The docs did not have an example for how to use this function, only an explanation of what it does, therefore I asked here in the hopes someone had one. I will try with providing an absolute path ins$$anonymous$$d, which if I'm not entirely incorrect can be done using the AssetDatabase helper. I am attempting to check if a folder exists in the folder tree, and then if it does exist, save a file inside of it. The code was irrelevant to the question other than the very line of code I provided.
I have done some researched and from what I have gathered, the relative path's should work according to this answer. Therefore I shouldn't be getting an array of size 0 when I try an get the subfolders of the Assets folder. Could it be because the script is executed in another folder and I need to change the path?
This answer is incorrect. You must provide a relative path, not an absolute one. This is shown in the docs through their working example of AssetDatabase.GetSubFolders("Assets");
.
The example script wasn't included in Unity's scripting documentation until version 2020.1, seeing how version 2019.4 does not include the example. With this in $$anonymous$$d, the function has also clearly been changed at some point.
While AssetDatabase.GetSubFolders() uses a relative path now, it used an absolute path in a previous version.
Edit: Looking back further, the change itself was made between versions 2017.1 and 2017.2.
Thanks, I've updated my answer with the relevant release notes of Unity 2017.1.3:
Asset Pipeline: AssetDatabase.GetSubfolders now returns paths correctly, and API documentation has been updated to reflect that it takes a relative path as an argument. (931944)
Thanks for narrowing down the version of the change ^^.
Answer by Torantulino · Aug 26, 2021 at 04:42 PM
As shown in the documentation, the AssetDatabase.GetSubFolders()
method DOES use a relative path, a common mistake however is to leave a trailing "/", which should not be provided.
Example correct usage: AssetDatabase.GetSubFolders("Assets/Resources/Textures");
Example INCORRECT usage: AssetDatabase.GetSubFolders("Assets/Resources/Textures/");
Your answer
![](https://koobas.hobune.stream/wayback/20220613200342im_/https://answers.unity.com/themes/thub/images/avi.jpg)