- Home /
How to find a path to a folder in the Assets folder?
How do I find the path to 'MyFolder' somewhere deep within the Assets folder? I've searched the Directory and AssetDatabase docs but can't find a way.
The closest is Directory.GetFiles(path), but that only lists the files within one given path.
Thanks
Do you know the entire exact path ? or do you want to search it ?
Personnally, I offered the choice to users to play their own music while they play, and they put their music files in a specific folder, which I access with :
var musicFolder:DirectoryInfo = new DirectoryInfo(Application.dataPath + "/$$anonymous$$usic");
Here are $$anonymous$$SDN doc and Unity doc I used to make this.
Thanks, but I want to find the path. I know the folder exists, but it's possible the user has moved it. The danger with your method is that it could return null if someone moves the folder.
Yes, but in the docs I gave you there is :
DirectoryInfo.EnumerateDirectories
And I think this is what you are looking for. Then you need to combine this with the other stuff (Application.dataPath to have the "root" folder).
EDIT : here
Thanks, that looked like a solution, but trying to use EnumerateDirectories gives an error: "`System.IO.DirectoryInfo' does not contain a definition for `EnumerateDirectories' ". Also, searching for EnumerateDirectories here returns no results. maybe Unity haven't implemented this? Are you able to call it successfully?
Answer by $$anonymous$$ · Nov 18, 2013 at 11:57 AM
You want to use Directory.GetDirectories() or Directory.GetFiles()
http://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx
You can create a recursive function if necessary to get to the deepest subdirectory level if necessary.
You can also use string substitution with string.Replace() to convert those paths into asset paths relative to the Assets directory.
I hope this helps!
"EnumerateDirectories" handles by itself the recursivity.
As per previous comment, EnumerateDirectories returns an error in Unity. Is it $$anonymous$$icrosoft only?
How did you use it ? (sorry, but I can't test it myself right now :) )
DirectoryInfo info = new DirectoryInfo(Application.dataPath);
var dirs = info.EnumerateDirectories();
But, YAY!, looking through the $$anonymous$$SDN docs I found a solution using GetFiles() that works:
string[] allPaths = Directory.GetFiles(Application.dataPath, "*", SearchOption.AllDirectories);
(That's using the global path, but easy to start from any directory)
EnumerateDirectories takes a string as argument I think
Your answer
Follow this Question
Related Questions
Does DirectoryInfo GetFiles work on Android ? 2 Answers
How To Refresh Resources Folder in Build? 1 Answer
Loading a ScriptableObject at Runtime. 1 Answer
Unable to Drag and Drop Folder/Assets into "Project" editor window. 2 Answers
Get UnityEngine.Object reference to folder in project from folder path 2 Answers