Is there a way to access a selected folder (in project view) through an editor script? If a folder is selected it will show up in the Selection.objects array, but how can I verify that it's a folder and get the complete path to it? I would like to write an editor script that performs some operations on all assets within a certain folder in my project and want to be able to choose that folder directly in the project view.
I just had to do something similar when trying to find a path for new assets. It's a little hacky for your usage but will get the job done without having to guess. I start with the assumption that each item is a directory and ask AssetDatabase.GenerateUniqueAssetPath(path) to give me a valid, unique path for a file inside that directory. If the item actually is a directory then a path is returned; but if it's a file then an empty string is returned.
for (var item in Selection.objects) {
var selpath = AssetDatabase.GetAssetPath(item);
if (selpath == "")
// not an asset
continue;
var dummypath = System.IO.Path.Combine(selpath, "fake.asset");
var assetpath = AssetDatabase.GenerateUniqueAssetPath(dummypath);
if (assetpath == "") {
// couldn't generate a path, current asset must be a file
Debug.Log("File: " + item.name);
}
else {
Debug.Log("Directory: " + item.name);
}
}
You need to use AssetDatabase.GetAssetPath() and Directory.Exists() which is part of the System.IO library in C#.
At the top of your script include the System.IO library:
using System.IO;
Then check your selections with the following:
foreach (Object obj in Selection.objects) {
string selectionPath = AssetDatabase.GetAssetPath(obj); // relative path
if (Directory.Exists(selectionPath)) {
// do something
}
}
Note that selectionPath is a relative path but Directory.Exists() is smart enough to realize this and still use it.
I just had the same problem. AssetDatabase.GetAssetPath(myObject) returns the folder path (or any other asset path). The real problem is to verify that it's a folder. I'm not sure but it seems that only folders are UnityEngine.Objects (everything in Unity is derived from it but a folder just have this type)
Just tried it that way:
foreach (UnityEngine.Object O in Selection.objects)
{
if (O.GetType() == typeof(UnityEngine.Object))
Debug.Log("Folder: " + AssetDatabase.GetAssetPath(O));
}
but unfortunately ".unity" files (scene files) are Objects too. Since folders can contain "." (dots) the only way to verify that it's a folder would be to build the complete path and use System.IO to check if it's a folder.
try this.
bool IsFolder(Object obj)
{
if (obj != null)
{
string asset_path = AssetDatabase.GetAssetPath(obj);
if (asset_path.Length > 0)
{
string file_path = Application.dataPath + "/" + asset_path.Replace("Assets/", "");
System.IO.FileAttributes file_attr = System.IO.File.GetAttributes(file_path);
if ((file_attr & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}