Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by ShodanIsAlive · Nov 16, 2015 at 10:26 AM · editorassetscustomselectionfolder

Custom Editor: show asset folders in ObjectField

Hello, is there a way to use ObjectField (or a more apt field) to show the list of Assets' subdirectories and pick one? I'd like to avoid using the open folder dialog provided by Unity (I don't remember the method name now) because I could open any path in my computer and I don't think it has parameters to limit the scope of selectable directories.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Piranha771 · Apr 09, 2018 at 09:29 AM

To provide an answer for this question. It is possible. Here is a working example editor window:

 using UnityEngine;
 using UnityEditor;
 
 class FolderTester: EditorWindow 
 {
   
     private DefaultAsset targetFolder = null;
 
     [MenuItem ("Window/Folder Selection Example")]
     public static void  ShowWindow () 
     {
         EditorWindow.GetWindow(typeof(FolderTester));
     }
     
     void OnGUI () 
     {
         targetFolder = (DefaultAsset)EditorGUILayout.ObjectField(
             "Select Folder", 
             targetFolder, 
             typeof(DefaultAsset), 
             false);
 
         if (targetFolder != null) {
             EditorGUILayout.HelpBox(
                 "Valid folder! Name: " + targetFolder.name, 
                 MessageType.Info, 
                 true);
         }
         else
         {
             EditorGUILayout.HelpBox(
                 "Not valid!", 
                 MessageType.Warning, 
                 true);
         }
         
     }
 }

window


111.png (21.9 kB)
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 Djayp · Nov 28, 2019 at 06:34 PM

You can limit the scope validating it after the folder has been chosen. Here is a complete PropertyDrawer I just made to keep folder references in the editor :

 using System.IO;
 using UnityEditor;
 using UnityEngine;
 
     [System.Serializable]
     public class FolderReference
     {
         public string guid;
     }
 
     [CustomPropertyDrawer(typeof(FolderReference))]
     public class FolderReferencePropertyDrawer : PropertyDrawer
     {
         private bool initialized;
         private SerializedProperty guid;
         private Object obj;
 
         private void Init(SerializedProperty property)
         {
             initialized = true;
             guid = property.FindPropertyRelative("guid");
             obj = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(guid.stringValue));
         }
         
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             if (!initialized) Init(property);
 
             GUIContent guiContent = EditorGUIUtility.ObjectContent(obj, typeof(DefaultAsset));
 
             Rect r = EditorGUI.PrefixLabel(position, label);
 
             Rect textFieldRect = r;
             textFieldRect.width -= 19f;
 
             GUIStyle textFieldStyle = new GUIStyle("TextField")
             {
                 imagePosition = obj ? ImagePosition.ImageLeft : ImagePosition.TextOnly
             };
 
             if (GUI.Button(textFieldRect, guiContent, textFieldStyle) && obj)
                 EditorGUIUtility.PingObject(obj);
 
             if (textFieldRect.Contains(Event.current.mousePosition))
             {
                 if (Event.current.type == EventType.DragUpdated)
                 {
                     Object reference = DragAndDrop.objectReferences[0];
                     string path = AssetDatabase.GetAssetPath(reference);
                     DragAndDrop.visualMode = Directory.Exists(path) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
                     Event.current.Use();
                 }
                 else if (Event.current.type == EventType.DragPerform)
                 {
                     Object reference = DragAndDrop.objectReferences[0];
                     string path = AssetDatabase.GetAssetPath(reference);
                     if (Directory.Exists(path))
                     {
                         obj = reference;
                         guid.stringValue = AssetDatabase.AssetPathToGUID(path);
                     }
                     Event.current.Use();
                 }
             }
 
             Rect objectFieldRect = r;
             objectFieldRect.x = textFieldRect.xMax + 1f;
             objectFieldRect.width = 19f;
 
             if (GUI.Button(objectFieldRect, "", GUI.skin.GetStyle("IN ObjectField")))
             {
                 string path = EditorUtility.OpenFolderPanel("Select a folder", "Assets", "");
                 if (path.Contains(Application.dataPath))
                 {
                     path = "Assets" + path.Substring(Application.dataPath.Length);
                     obj = AssetDatabase.LoadAssetAtPath(path, typeof(DefaultAsset));
                     guid.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(obj));
                 }
                 else Debug.LogError("The path must be in the Assets folder");
             }
         }
     }
Comment
Add comment · Show 2 · 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 Elledan3101 · Mar 04, 2021 at 01:51 PM 0
Share

thanks a lot! this was exactly what I looked for!

avatar image zhaozony · May 26, 2021 at 03:36 AM 0
Share

How to use this Folder Reference code? alt text

i5juwyvblyqo.png (73.5 kB)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting TextAsset[] from an Editor 0 Answers

Select asset for rename 2 Answers

editor folder path on Mac 0 Answers

Working with the editor Selection 0 Answers

How to get the top most GameObject selected in Editor 0 Answers


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