x


get folder from selection array

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.

more ▼

asked Feb 10 '11 at 02:00 PM

StephanK gravatar image

StephanK
6k 39 53 93

(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

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.

more ▼

answered Nov 09 '12 at 10:04 AM

Soviut gravatar image

Soviut
91 4 6 13

(comments are locked)
10|3000 characters needed characters left

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);
    }
}
more ▼

answered Apr 19 '11 at 12:58 AM

loramaru gravatar image

loramaru
404 1 6

(comments are locked)
10|3000 characters needed characters left

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 http://System.IO to check if it's a folder.

more ▼

answered Apr 04 '11 at 01:27 PM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x1675
x349
x130

asked: Feb 10 '11 at 02:00 PM

Seen: 3033 times

Last Updated: Nov 09 '12 at 10:07 AM