Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by StephanK · Feb 10, 2011 at 02:00 PM · editoreditor-scriptingselection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by loramaru · Apr 19, 2011 at 12:58 AM

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

Answer by Soviut · Nov 09, 2012 at 10:04 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Bunny83 · Apr 04, 2011 at 01:27 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

Answer by hollycq · Sep 03, 2013 at 07:06 AM

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;
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cannot set Selection.activeObject (no errors, no clues) 0 Answers

Updating object on inspector value changes in editor 1 Answer

How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer

How to check if a key is down in editor script 2 Answers

Highlighting / Focusing on an Editor Window Through Code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges